将一个集合的数据作为子文档获取另一个文档mongoDb

时间:2017-07-26 06:38:56

标签: node.js mongodb mongoose

我正在和猫鼬一起工作。我有一个meta这样的集合

{
    "_id" : ObjectId("597568f5cf12434674"),
    "data": "test data",
    "createBy" : "testing",
    "modifyBy" : "testing",
    "modifyDate" : ISODate("2017-07-24T03:26:45.350Z"),
    "createDate" : ISODate("2017-07-24T03:26:45.350Z"),
}

_id位于名为orders的另一个名为metaId

的集合中
{
    "_id" : ObjectId("597818bf5332f91213411"),
    "type" : "food order",
    "isActive" : true,
    "files" : [ 
        {
            "metaId" : "597568f5cf12434674",
            "_id" : ObjectId("597818sdfasdaf2222")
        }
    ]
}

当我收到订单详细信息时,我需要加入这两个集合,所以我的最终结果将是这样的

{
    "_id" : ObjectId("597818bf5332f91213411"),
    "type" : "food order",
    "isActive" : true,
    "files" : [ 
        {
            "metaId" : "597568f5cf12434674",
            "data": "test data",
            "createBy" : "testing",
            "modifyBy" : "testing",
            "modifyDate" : ISODate("2017-07-24T03:26:45.350Z"),
            "createDate" : ISODate("2017-07-24T03:26:45.350Z"),
            "_id" : ObjectId("597818sdfasdaf2222")
        }
    ]
}

我写了这个查询但是返回空,我知道这是不正确的,因为我希望元数据在订单内。

const query = db.orders.aggregate([  
      {'$match': {'_id': _id}},
      {'$lookup': {
        'localField': 'files.metaId',
        'from': 'meta',
        'foreignField': '_id',
        'as': 'metaInfo'
      }},
      {'$unwind': '$metaInfo'},
      {'$project': {
        'type': 1,
        'isActive': 1,
        'metaInfo.data': 1
      }}
    ]);

无论如何,我可以做这个mongo,我对mongodb很新。请提前帮助,谢谢。

1 个答案:

答案 0 :(得分:1)

$lookup聚合运算符无法使用'来自'因此你必须首先解开它:

db.getCollection('orders').aggregate([  
  // matches a single document in the orders collection
  {'$match': {'_id': ObjectId("59784eb56149554af36a5666")}},

  // unwinds orders.files so that it is no longer an array
  {'$unwind': '$files'},

  // performs the lookup on the meta collection and assigns the 'looked up' sub document to the attribute: "files"
  {'$lookup': {
    'localField': 'files.metaId',
    'from': 'meta',
    'foreignField': '_id',
    'as': 'files'
  }}
]);

上述命令将返回以下文档:

{
    "_id" : ObjectId("59784eb56149554af36a5666"),
    "type" : "food order",
    "isActive" : true,
    "files" : [ 
        {
            "_id" : ObjectId("59784e706149554af36a5621"),
            "data" : "test data",
            "createBy" : "testing",
            "modifyBy" : "testing",
            "modifyDate" : ISODate("2017-07-24T03:26:45.350Z"),
            "createDate" : ISODate("2017-07-24T03:26:45.350Z")
        }
    ]
}

注意:这会使用查找的内容文档填充files属性'从元集合中,您丢失了files.metaId,但由于从orders.files.metaIdmeta._id的连接,文件子文档中的_id值与 orders.files.metaId。如果稍微更改命令,则可以看到此信息:

  db.getCollection('orders').aggregate([  
    // matches a single document in the orders collection
    {'$match': {'_id': ObjectId("59784eb56149554af36a5666")}},

    // unwinds orders.files so that it is no longer an array
    {'$unwind': '$files'},

    // performs the lookup on the meta collection and assigns the 'looked up' sub document to the attribute: "files"
    {'$lookup': {
      'localField': 'files.metaId',
      'from': 'meta',
      'foreignField': '_id',
      'as': 'metaData'
    }}
  ]);

返回:

{
    "_id" : ObjectId("59784eb56149554af36a5666"),
    "type" : "food order",
    "isActive" : true,
    "files" : {
        "metaId" : ObjectId("59784e706149554af36a5621")
    },
    "metaData" : [ 
        {
            "_id" : ObjectId("59784e706149554af36a5621"),
            "data" : "test data",
            "createBy" : "testing",
            "modifyBy" : "testing",
            "modifyDate" : ISODate("2017-07-24T03:26:45.350Z"),
            "createDate" : ISODate("2017-07-24T03:26:45.350Z")
        }
    ]
}

其中files.metaId == metaData._id