选择蒙戈DB嵌套字段
问题描述:
我在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"
}
}
这是可能的?
答
你需要如下汇总:
- 构造一个
find
对象只匹配包含($exists)语言的记录。 - 构建一个
Projection
对象来投影这些字段。
代码:
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}
])
答
我不知道你是如何查询,所以我会假设你直接通过客户端蒙哥去。假设你已经定义的变量
>currentLang = "en";
您可以运行aggregation operation并使用$project operator,重组文档的表现。
下面是我测试过的例子:
> db.test.aggregate({$project:
{_id: 0,
title: "$" + currentLang + ".title",
content: "$" + currentLang + ".content",
images: 1
}
}).pretty();
{
"images" : {
"mainImage" : "dataURL",
"thumbImage" : "dataURL"
},
"title" : "eng title",
"content" : "eng content"
}
如果你想这与实际查询相结合,你可以把它作为一个$match operator,是这样的:
> db.test.aggregate(
{$match:
{"en.title": "eng title"}
},
{$project:
{_id: 0,
title: "$" + currentLang + ".title",
content: "$" + currentLang + ".content",
images: 1
}
}).pretty();
这正是我现在使用的代码;但输出文档仍然嵌套在“en”下;我想在根元素的所有领域,如上面写的所需的输出 – 2014-12-05 22:53:55
@CerealKiller然后你需要聚合,投影带有别名的字段。请参阅最新的答案。 – BatScream 2014-12-05 22:59:23
好吧,这是正确的;不幸的是,我在Meteor.js里面使用了mongo,其中聚合似乎不被支持;对不起,当我写这个问题,我不知道... – 2014-12-05 23:26:34