我在MongoDB中一个集合,其中字段嵌套在语言根目录下:选择蒙戈DB嵌套字段
{
en: {
title: "eng title",
content: "eng content",
},
it: {
title: "it title",
content: "it content"
}
//common attributes for all languages
images: {
mainImage: "dataURL",
thumbImage: "dataURL"
}
}
我有一个叫做“currentLang”变量;我需要通过标题找到一个文档,只选择“currentLang”对象和公共字段(本例中的图像); 但对于“currentLang”对象,我希望输出文档不嵌套;例如,具有currentLang = “EN”
期望的输出:
{
title: "eng title",
content: "eng content",
images: {
mainImage: "dataURL",
thumbImage: "dataURL"
}
}
这是可能的?
这正是我现在使用的代码;但输出文档仍然嵌套在“en”下;我想在根元素的所有领域,如上面写的所需的输出 – 2014-12-05 22:53:55
@CerealKiller然后你需要聚合,投影带有别名的字段。请参阅最新的答案。 – BatScream 2014-12-05 22:59:23
好吧,这是正确的;不幸的是,我在Meteor.js里面使用了mongo,其中聚合似乎不被支持;对不起,当我写这个问题,我不知道... – 2014-12-05 23:26:34