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.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度