如何在没有$ unwind的情况下加入三个集合并根据条件获取嵌套结果?

武士杰克

人员集合:

[
  {
    "_id": ObjectId("5f3258cfbaaccedaa5dd2c96"),
    "gender": "male",
    "name": {
      "title": "mr",
      "first": "victor",
      "last": "pedersen"
    },
    "location": {
      "street": "2156 stenbjergvej",
      "city": "billum",
      "state": "nordjylland",
      "postcode": 56649        
    },
    "email": "[email protected]"
  }
]

PersonDetails集合:

{
    "_id": ObjectId("5f3a91e68b1c26e68f9ed3ad"),
    "country": "India",
    "personid": ObjectId("5f3258cfbaaccedaa5dd2c96")
}

CountryDetails集合:

{
    "_id": ObjectId("5f3fc2aa9532398a037ff7ae"),
    "country": "India",
    "continent": "Asia"
}

假设1个人可以有很多个人详细信息,而1个人可以有很多国家/地区详细信息。

查询:获取大陆为亚洲的人员,人员详细信息和国家/地区详细信息。

结果应如下所示:

[{
    "_id": ObjectId("5f3258cfbaaccedaa5dd2c96"),
    "gender": "male",
    "name": {
        "title": "mr",
        "first": "victor",
        "last": "pedersen"
    },
    "location": {
        "street": "2156 stenbjergvej",
        "city": "billum",
        "state": "nordjylland",
        "postcode": 56649
    },
    "email": "[email protected]",
    "persondetail": [{
        "_id": ObjectId("5f3a91e68b1c26e68f9ed3ad"),
        "country": "India",
        "personid": "5f3258cfbaaccedaa5dd2c96",
        "countrydetail": [{
            "_id": ObjectId("5f3fc2aa9532398a037ff7ae"),
            "country": "India",
            "continent": "Asia"
        }]
    }]
}]

注意:必须与aggregate()一起使用

我的失败尝试:

db.persons.aggregate([
  {
    "$match": {
      "$expr": {
        "$eq": [
          "$_id",
          {
            "$toObjectId": "5f3258cfbaaccedaa5dd2c96"
          }
        ]
      }
    }
  },
  {
    $lookup: {
      from: "persondetails",
      localField: "_id",
      foreignField: "personid",
      as: "persondetail"
    }
  },
  {
    $unwind: {
      "path": "$persondetail",
      includeArrayIndex: "arrayIndex",
      preserveNullAndEmptyArrays: true
    }
  },
  {
    $lookup: {
      from: "country",
      localField: "persondetail.country",
      foreignField: "country",
      as: "countrydetails"
    }
  },
  {
    "$match": {
      "$expr": {
        "$eq": [
          "$persondetail.continent",
          "Asia"
        ]
      }
    }
  }
])
 

上面的查询不起作用,即使它起作用了,unwind也会给我一个扁平的结构。这与我要寻找的相反。

米克尔

您可以使用$ lookup和自定义管道来嵌套查询

{
    $lookup: {
       from: "persondetails",
       let: { person_id: "$_id" },
       pipeline: [
           {
               $match: {
                   $expr: { $eq: [ "$personid", "$$person_id" ] }
               }
           },
           {
               $lookup: {
                   from: "countrydetails",
                   localField: "country",
                   foreignField: "country",
                   as: "countrydetail",
               }
           }
       ],      
       as: "persondetail"
    }
}

蒙哥运动场

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在没有默认“排序”的情况下加入两个集合

如何在没有NSFetchedResultsControllerDelegate的情况下获取结果?

如何在没有公共“GROUP BY”元素的情况下合并 2 个查询的结果

如何在没有教条关系的情况下加入?

如何在没有ON的情况下加入表格

如何在没有findall的情况下对结果求和

如何在没有NaN的情况下合并groupby结果?

如何在没有第二个LINQ查询的情况下获取排除的集合?

如何在没有集合的情况下从列表中查询一个对象?

Tensorflow:如何在没有np.where的情况下根据条件随机选择元素?

您如何在没有寻呼机的情况下获取git grep返回结果?

如何在没有阻塞调用的情况下从 IObservable 订阅中获取结果?

如何在没有任何条件的情况下链接两个选择?

如何在没有箭头键的情况下获取上一个命令

如何在没有uuid的情况下编译fontconfig

如何在没有 LINQ 的情况下添加小计

如何在没有ObjectNode的情况下创建ValueNode?

如何在没有tty的情况下模拟外壳?

如何在没有sudo的情况下执行'iftop'

如何在没有sudo的情况下安装库?

如何在没有WebSocket的情况下进行通知

如何在没有注释的情况下使用 swagger

如何在没有if语句的情况下做出决定

如何在没有桌面环境的情况下启动?

如何在没有父母的情况下对DIV居中?

如何在没有按扣的情况下安装Chromium?

如何在没有.Net 的情况下制作项目?

如何在没有+的情况下加数字

如何在没有sudo的情况下构建库?