I have a collection in mongodb where fields are nested under a language root:
{
en: {
title: \"eng title\",
content: \"eng content\",
You need to aggregate as below:
find
object to match only the records containing($exists) the language.Projection
object to project the 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}
])