mongodb - Select nested fields in mongo db -


i have collection in mongodb fields nested under language root:

{     en: {         title: "eng title",         content: "eng content",     },     it: {         title: "it title",         content: "it content"     }     //common attributes languages     images: {         mainimage: "dataurl",         thumbimage: "dataurl"     } } 

i have variable called 'currentlang'; need find document title selecting "currentlang" object , common fields (images in example); "currentlang" object, have output document not nested; example, having currentlang = "en"

desired output:

{     title: "eng title",     content: "eng content",     images: {         mainimage: "dataurl",         thumbimage: "dataurl"     } } 

is possible?

you need aggregate below:

  • construct find object match records containing($exists) language.
  • construct projection object project fields.

code:

var currentlang = "en"; var project = {}; project["title"] = "$"+currentlang+".title"; project["content"] = "$"+currentlang+".content"; project["images"] = 1;  var find = {}; find[currentlang] = {"$exists":true};  db.collection.aggregate([ {$match:find}, {$project:project} ]) 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -