【问题标题】:mongodb joining two table with multiple arraymongodb 连接两个具有多个数组的表
【发布时间】:2019-10-25 12:58:25
【问题描述】:

我有一个表成员资格,另一个是促销代码。

成员表:

/* 1 */
{
    "_id" : ObjectId("5b62e7050b13587e9febb8db"),
    "country_id" : "5a093507f1d45633844096ef",
    "type" : "Bronze",
    "price" : "30",
}
/* 2 */
{
    "_id" : ObjectId("5b6961175140b477032291b7"),
    "country_id" : "5a093507f1d45633844096ef",
    "type" : "Gold",
    "price" : "1000",
}....

促销代码表:

{
    "_id" : ObjectId("5cfe23fd075be65883f6d921"),
    "final_status" : 1,
    "membership" : [ 
        ObjectId("5b62e7050b13587e9febb8db"), 
        ObjectId("5b6961175140b477032291b7"), 
        ObjectId("5b6961285140b477032291b8"), 
        ObjectId("5b7567dd5b874856981b53d3"), 
        ObjectId("5bba1c3794c6761db256edbc")
    ],
    "month" : [ 
        "1", 
        "3"
    ]
}

这里,promomcode 表有一个会员ID。我想要带有会员加入查询的促销代码数据。获取带有会员详细信息的促销代码数据 请帮帮我

【问题讨论】:

标签: mongodb mongoose mongodb-query aggregation-framework


【解决方案1】:

在 mongodb 中使用 $lookup 进行左外连接

db.promocode.aggregate([{
  $lookup: {
    from: 'membership',
    localField: 'membership',
    foreignField: '_id',
    as: 'memberships'
  }
}, {
  $project: {
    membership: 0
  }
}])

【讨论】:

    猜你喜欢
    • 2014-03-06
    • 1970-01-01
    • 1970-01-01
    • 2015-12-27
    • 2017-10-19
    • 2016-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多