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

javascript - How to synchronize the Three.js and HTML/SVG coordinate systems (especially w.r.t. the y-axis)? -

javascript - How do I find how many occurences are there of a highlighted string, and which occurence is it? -

java - Reading data from multiple zip files and combining them to one -