Du skal samle som nedenfor:
- Konstruer et
find
objekt for kun at matche de poster, der indeholder($exists) sproget. - Konstruer en
Projection
objekt for at projicere felterne.
Kode:
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}
])