【发布时间】:2018-10-04 14:16:34
【问题描述】:
我正在使用 MongoDB 3.4。
有2个收藏如下。
集合 1:- 类型
{
"_id": {
"$numberLong": "1234"
},
"name" : "board1"
"type" : "electronic"
},
{
"_id": {
"$numberLong": "1235"
},
"name" : "board2",
"type" : "electronic"
}
系列 2:- 产品
{
"_id": {
"$numberLong": "9876"
},
"types" : [
"1234",
"1235",
"1238"
]
},
{
"_id": {
"$numberLong": "9875"
},
"types" : [
"1234",
"1238"
]
}
类型集合会有多种类型,产品集合中的每个产品都有多种类型。
类型集合中的同一类型可以有多个具有不同 id 的文档。并且,产品集合可能具有具有相同类型或不同类型的不同 Id 的类型数组。
我想获取电子类型的所有 id,并在每个产品的类型数组中找到具有 id 的产品。
我想要下面这样的结果。
{
"_id": {
"$numberLong": "1234"
},
"name" : "board1",
"products" : [
"9876",
"9875"
]
},
{
"_id": {
"$numberLong": "1235"
},
"name" : "board2"
"products" : [
"9876",
"9875"
]
}
目前,我打了很多电话,比如为每个类型 id 获取所有产品。
有没有其他使用 $lookup 或任何其他机制的单一查询的简单方法?
【问题讨论】:
标签: mongodb aggregation-framework