【问题标题】:Join Query in MongoDB [duplicate]在 MongoDB 中加入查询 [重复]
【发布时间】:2019-08-17 13:15:51
【问题描述】:


我正在学习 MongoDB,并且对 NoSQL 非常陌生。
我有 2 个这样的收藏:
客户:

帐号:

我想通过客户集合中的 _id 和帐户集合中的 cust_id 创建客户和帐户集合之间的连接查询。我试过这个:

db.Customer.aggregate({
    $lookup:{
        from:"Account",
        localField:"_id",
        foreignField:"cust_id",
        as:"customer_account"
}}).pretty()

但我的“customer_account”返回一个空数组。我该如何解决这个问题?非常感谢!

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    Account 文档中的cust_id 字段属于string 类型(您可能从双引号中看到)。

    您需要将其更改为ObjectId 类型。所以cust_id: "5499..."会变成cust_id: ObjectId("5499...")

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-14
      • 2011-06-01
      • 2019-03-16
      • 2014-06-01
      • 1970-01-01
      • 2018-11-08
      相关资源
      最近更新 更多