说我有以下文件
项目
{
details: [
{
"title": "Name",
"content": "",
"is_private": false,
"order": 0,
"type": "text"
},
{
"title": "Price",
"content": "",
"is_private": true,
"order": 1,
"type": "text"
},
{
"title": "Company",
"content": "",
"is_private": false,
"order": 2,
"type": "text"
}
],
}
如果我只想返回包含details
的子文档字段is_private === false
,在猫鼬的查询中是否有办法做到这一点,还是我需要使用聚合?
例如
ItemModel
.find()
// something should go here to remove
答案 0 :(得分:1)
如果您想使用本机聚合查询来做到这一点:
db.getCollection('item').aggregate([
{
$unwind:"$details"
},
{
$match:{
"details.is_private":true
}
},
{
$group:{
_id:"$_id",
details:{
$push:"$details"
}
}
}
])
输出:
{
"_id" : ObjectId("5b7d0edb6cfaf771ecf675f0"),
"details" : [
{
"title" : "Price",
"content" : "",
"is_private" : true,
"order" : 1.0,
"type" : "text"
}
]
}