MongoDB Aggregation Framework : Using dynamic javascript variables in Project "key" field -


A newbie is enough for the mongodibi aggregation framework to think that in a holistic framework, a project can use dynamic variables There is a way.

Example This is my code, it is not for Javascript syntax, but if you get this thing:

  // My javascript variable var my_variable = "salary" ; // db.article.aggregate ({$ Project: {title: 1, author: 1,}});   

Now, in the code given above, I want to dynamically change the projection of "author" of "pay". To do something like this

  db.article.aggregate ({$ project: {title: 1, "my_variable": 1,}});   

who will project the salary dynamically in this case.

Your variable is not really dynamic here because this aggregation can not change during the pipeline. The aggregation framework does not evaluate javascript, so whatever you have been provided should be obtained from the pipeline that is provided or the document is being processed.

However, you can certainly make the pipeline a program to get your results. You can create a variable to represent some (or all) of your aggregation pipeline using for example:

  var MyProjection = {Title: 1, Author: 1,} // Change Author with myProjection.author myProjection.salary = 1 // Projection: {"Title": 1, "Salary": 1} db.article. In this case, the  myProjection  variable is sent to the integration query before the  mongo  the  Being evaluated by shell MongoDB server is available   

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -