$ lookup与嵌套的对象数组 [英] $lookup with nested array of objects

查看:61
本文介绍了$ lookup与嵌套的对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文档具有包含多个对象的数组,并且我需要找到相关值并将其推入同一数组.

My document has array with multiple objects, And I need to find related values and push into same array.

购物车

{
    "_id" : ObjectId("5b2b72119fbb60750e0061b9"),
    "cartId" : "1529573905701",
    "supplierId" : ObjectId("5b221d1b63eda2902418434d"),
    "user_id" : "5b20c54651e68057b3cbe745",
    "createdAt" : ISODate("2018-06-21T09:38:25.680Z"),
    "services" : [ 
        {
            "date" : "21-06-2018",
            "timeSlot_id" : ObjectId("5b29e08cb116c31f5b1f56c6"),
            "time" : "03:30 PM - 04:30 PM",
            "serviceId" : ObjectId("5b24aff4abf2494701bc1c15"),
            "cost" : 250,
            "_id" : ObjectId("5b2b72119fbb60750e0061ba")
        }, 
        {
            "_id" : ObjectId("5b2b72329fbb60750e0061bb"),
            "cost" : 250,
            "serviceId" : ObjectId("5b24aff4abf2494701bc1c15"),
            "time" : "03:30 PM - 04:30 PM",
            "timeSlot_id" : ObjectId("5b29e08cb116c31f5b1f56c6"),
            "date" : "21-06-2018"
        }
    ],
    "__v" : 0
} 

服务

{
    "_id" : ObjectId("5b24aff4abf2494701bc1c15"),
    "isActivated" : true,
    "supplierServiceId" : 900146649,
    "serviceId" : 99473640,
    "serviceName" : "AC Reparing",
    "description" : "all type of AC, Split AC and Window AC",
    "cost" : 250,
    "serviceDuration" : "60",
    "startTime" : "07:00 AM",
    "endTime" : "08:30 PM",
    "supplierId" : "5b221d1b63eda2902418434d",
    "createdAt" : ISODate("2018-06-16T06:36:36.091Z"),
    "__v" : 0
}

时隙

{
    "_id" : ObjectId("5b29e08cb116c31f5b1f56c6"),
    "displayString" : "03:30 PM - 04:30 PM",
    "isActivated" : true,
    "startTime" : "15:30 PM",
    "endTime" : "16:30 PM",
    "duration" : "60",
    "createdAt" : ISODate("2018-06-20T05:05:16.405Z"),
    "__v" : 0
}

CART 中的服务内部有两个值 serviceId timeSlot_id

Inside the services in CART there are two values serviceId and timeSlot_id

*我需要与相关集合中的两个相关的数据.在同一对象内.

*I need data related to both from related collections. Within same object.

Mongo版本-v3.6.5

Mongo version -v3.6.5

请帮助解决此问题.预先感谢.

Please Help to solve this. Thanks in advance.

推荐答案

您需要

You need to $unwind the services in order to match its timeSlot_id and serviceId with the _id of another collection of TimeSlots and ServiceId respectively... And then you can $group it to start the process of "rolling back" into the arrays.

如果您拥有mongodb版本 3.6

If you have mongodb version 3.6

Cart.aggregate([
  { "$match": { "user_id": _user._id } },
  { "$unwind": "$services" },
  { "$lookup": {
    "from": TimeSlot.collection.name,
    "let": { "timeSlot_id": "$services.timeSlot_id" },
    "pipeline": [
       { "$match": { "$expr": { "$eq": [ "$_id", "$$timeSlot_id" ] } } }
     ],
     "as": "services.timeSlot_id"
  }},
  { "$lookup": {
    "from": Service.collection.name,
    "let": { "serviceId": "$services.serviceId" },
    "pipeline": [
       { "$match": { "$expr": { "$eq": [ "$_id", "$$serviceId" ] } } }
     ],
     "as": "services.serviceId"
  }},
  { "$unwind": "$services.timeSlot_id" },
  { "$unwind": "$services.serviceId" },
  { "$group": {
    "_id": "$_id",
    "services": { "$push": "$services" },
    "cartId" : { "$first": "$cartId" },
    "supplierId" : { "$first": "$supplierId" },
    "user_id" : { "$first": "$user_id" },
    "createdAt" : { "$first": "$createdAt" }
  }}
])

如果您的mongodb版本低于 3.6

If you have mongodb version prior to 3.6

Cart.aggregate([
  { "$match": { "user_id": _user._id } },
  { "$unwind": "$services" },
  { "$lookup": {
    "from": TimeSlot.collection.name,
    "localField": "services.timeSlot_id",
    "foreignField": "_id",
    "as": "services.timeSlot_id",
  }},
  { "$lookup": {
    "from": Service.collection.name,
    "localField": "services.serviceId",
    "foreignField": "_id",
    "as": "services.serviceId",
  }},
  { "$unwind": "$services.timeSlot_id" },
  { "$unwind": "$services.serviceId" },
  { "$group": {
    "_id": "$_id",
    "services": { "$push": "$services" },
    "cartId" : { "$first": "$cartId" },
    "supplierId" : { "$first": "$supplierId" },
    "user_id" : { "$first": "$user_id" },
    "createdAt" : { "$first": "$createdAt" }
  }}
])

这篇关于$ lookup与嵌套的对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆