douxiexie3574 2019-06-24 07:57
浏览 106

如何在嵌入式文档中查找,嵌入式文档使用mongodb具有id

I'm using mongoDb in my project, and I have problem using lookup on embedded data.. I searched for my problem and read similar codes.. but they didn't help my problem..

I mean in my embed document there is a variable named user that I want to load its data by lookup..

this is how my documents is:

"_id" : ObjectId("5d0f6a993702da22bc00474d"),
"quotes" : [
   {
     "amount" : NumberInt(100), 
     "user" : {
        "_id" : ObjectId("5cc69f7f46ab4cedda5c3c12")
     }, 
     "details" : "some quote details", 
     "_id" : ObjectId("5d0f6a993702da22bc00474c"), 
     "status" : "a"
    },
    {
      "amount" : NumberInt(120), 
      "user" : {
         "_id" : ObjectId("5cc69kij46ab4cedda5c3c12")
      }, 
      "details" : "some quote details 2", 
      "_id" : ObjectId("5d0f6a993702da22bc00484k"), 
      "status" : "a"
     }
],
"status" : "Approved", 
"title" : "gardening", 
"created_at" : NumberInt(1560841417)

so I want my output be like this:

"_id" : ObjectId("5d0f6a993702da22bc00474d"),
"quotes" : [
        {
            "amount" : NumberInt(100), 
            "user" : {
                "_id" : ObjectId("5cc69f7f46ab4cedda5c3c12"),
                "firstName": "John",
                "lastNAme": "Doe",
                "phone": 514298742
            }, 
            "details" : "some quote details", 
            "_id" : ObjectId("5d0f6a993702da22bc00474c"), 
            "status" : "a"
        },
        {
            "amount" : NumberInt(120), 
            "user" : {
                "_id" : ObjectId("5cc69kij46ab4cedda5c3c12")           
                "firstName": "Jane",
                "lastNAme": "Doe",
                "phone": 5148547642
            }, 
            "details" : "some quote details 2", 
            "_id" : ObjectId("5d0f6a993702da22bc00484k"), 
            "status" : "a"
        }
],
"status" : "Approved", 
"title" : "gardening", 
"created_at" : NumberInt(1560841417)
  • 写回答

1条回答 默认 最新

  • duankan6894 2019-06-24 11:06
    关注

    I found the solution.. this worked for me..

    .aggregate([
      {
        {
          "$unwind": {
            "path": "$quotes",
            "preserveNullAndEmptyArrays": true
           }
        },
        {
           "$lookup": {
             "from": "users",
             "localField": "quotes.user._id",
             "foreignField": "_id",
             "as": "quotes.user",
            }
        },
        {
            "$unwind": {
               "path": "$quotes.user",
               "preserveNullAndEmptyArrays": true
             }
         },
         {
            "$group": {
               "_id": null,
               "quotes": {"$push": "$quotes"},
               "status": {"$first": "$status"},
               "title": {"$first": "$title"},
               "created_at": {"$first": "$created_at"},
             }
         },
      }
    ])
    

    I posted my answer if anyone have the same issue.

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向