【发布时间】:2019-05-01 14:57:00
【问题描述】:
我正在尝试单独获取所有匹配的元素,here 是示例数据和查询。
// json
[
{
"name": "Mr Cool",
"ican": [
{
"subcategory": [
{
"id": "5bffdba824488b182ec86f8d", "name": "Cricket"
},
{
"id": "5bffdba824488b182ec86f8c", "name": "Footbal"
}
],
"category": "5bffdba824488b182ec86f88",
"name": "Sports"
}
]
}
]
// query
db.collection.aggregate([
{
"$match": {
"ican.subcategory.name": { $in: ["Cricket","Football"] }
}
},
{
"$project": { "_id": 1, "name": 1, }
}
])
我得到了综合结果,我需要个人比赛记录。我尝试了$all 和$elementMatch,但得到了相同的响应。我怎样才能得到如下结果。我正在使用$aggregate,因为我将使用$geoNear 管道来获取附近的用户。
// current result
[
{
"_id": ObjectId("5a934e000102030405000000"),
"name": "Mr Cool"
}
]
// expected result
[
{
"_id": ObjectId("5a934e000102030405000000"),
"name": "Mr Cool",
"subcategory: "Cricket"
},
{
"_id": ObjectId("5a934e000102030405000000"),
"name": "Mr Cool",
"subcategory: "Footbal"
}
]
谢谢
【问题讨论】:
-
如果
ican也有多个元素怎么办? -
是的,它会有多个元素。
标签: mongodb mongoose nosql mongodb-query aggregation-framework