我有n个这样的文档(摘录,还有更多字段,但这是基本结构):
{
"_id": "someId",
"someField": "someValue", // used for matching
// a couple of more fields
"nestedObject": {
"val1": "blabla", // used for matching
"val2": "group on the string content here"
// a couple of more fields
}
}
以此类推。
我想导出此集合,但删除nestedObject.val2
具有相同内容的文档。但是我需要每个文档的每个字段。
这是我微不足道的尝试:
db.getCollection("somCol").aggregate(
[
{
"$match" : {
"$and" : [
{
"someField" : "someValue"
},
{
"nestedObject.val1" : "blabla"
}
]
}
},
{
"$unwind" : {
"path" : "$nestedObject"
}
},
{ // not sure here
"$group" : {
"_id" : "$nestedObject.val2",
"content" : { // here I would like the entire document. Do I need to add each field separately? There are more fields than I posted above
"$addToSet" : "$nestedObject.val2"
}
}
}
],
{
"allowDiskUse" : false
}
)
我认为该分组实际上有效,但是,是...无法弄清楚如何返回整个文档。