mongodb之lookup单条件关联

 

select * from MiscShipRecord 
left join DeliveryRecord on MiscShipRecord._id=new ObjectId(DeliveryRecord.SourceEntityID)

Mongodb实现方式:

db.MiscShipRecord.aggregate([
    { $match : { _id : new ObjectId("62fb05d7e4ca8700257fad96") } }, // where MiscShipRecord._id=new ObjectId("62fb05d7e4ca8700257fad96")
    { $addFields: { "MiscShipRecordId": { "$toString": "$_id" }}}, // toString(MiscShipRecord._id) as MiscShipRecordId
    {
             $lookup:
                 {
                     from: "DeliveryRecord",
                     localField: "MiscShipRecordId",
                     foreignField: "SourceEntityID",
                     as: "DeliveryRecord_docs"
                 }
    },                                                                                                    // DeliveryRecord.SourceEntityID=MiscShipRecordId
    {
      $unwind: "$DeliveryRecord_docs"                                    // 展开
  }
])

 

posted @ 2024-05-11 10:55  江境纣州  阅读(1)  评论(0编辑  收藏  举报