谢谢
我一直在尝试针对Atlas中的mongoDB集合编写一个独特的查询。
我想获得在"section"中捕获的所有不同值的列表。属性下的"元数据"属性。父属性。根据mongoDB文档,获取不同值数组的语法如下:
{ distinct: "<collection>", key: "<field>" }
我的样本集叫做"simple"保存以下文档
[{
"_id": "527c61082241f813c09c722c",
"MetaData": {
"Type": "BlogPost",
"Author": "author1",
"Section": "section1"
},
"Title": "title 1",
"Description": "..."
},
{
"_id": "527c61082241f813c09c7050",
"MetaData": {
"Type": "BlogPost",
"Author": "author1",
"Section": "section1"
},
"Title": "title 2",
"Description": "..."
},
{
"_id": "527c61082241f813c09c7042",
"MetaData": {
"Type": "BlogPost",
"Author": "author1",
"Section": "section2"
},
"Title": "title 3",
"Description": "..."
}
]
但是当我在Compass或Atlas中执行以下过滤器查询时-没有返回结果??
{distinct: 'simple',key: 'MetaData.Section'}
我期待一个数组保存"section1"one_answers"section2">
有人能告诉我做错了什么吗?
谢谢
您需要为compass使用聚合框架来获取不同的值
{
$group:{
"_id": null,
"sections": { $addToSet: "$MetaData.Section"}
}
}