mongoDB在对象嵌套数组上聚合查找

时间:2019-09-24 14:32:17

标签: arrays mongodb mongoose aggregation-framework

我有一个看起来像下面的集合,我很难执行$ lookup并以开始时的方式返回它,但要填充字段:

我对我要填充的字段(代理,missions.clients.client)进行了评论

 {  
   "title":"Tournée libre",
   "agent":"5d811943d2a2100017667228", // needs to be populated
   "missions":[  
      {  
         "_id":"5d8a075346f10d679ab4383e",
         "title":"Journée 3",
         "clients":[  
            {  
               "_id":"5d8a075346f10d679ab4383f",
               "valid":true,
               "client":"5d1bc39aa2af623b94363b33", // this needs to be populated
               "visit_time":"2019-09-24T12:03:38.383Z"
            },
            {  

               "_id":"5d8a0dc446f10d679ab43888",
               "valid":true,
               "client":"5d8a0c8346f10d679ab43886",
               "visit_time":"2019-09-24T12:34:23.210Z"
            },

         ]
      }
   ],
   "created_at":"2019-09-24T12:08:51.928Z",
   "__v":2
}

结果如下:

 {  
   "title":"Tournée libre",
   "agent": {firstname: 'something', lastname: 'something else'}
   "missions":[  
      {  
         "_id":"5d8a075346f10d679ab4383e",
         "title":"Journée 3",
         "clients":[  
            {  
               "_id":"5d8a075346f10d679ab4383f",
               "valid":true,
               "client": {firstname: 'something', lastname: 'something else'},
               "visit_time":"2019-09-24T12:03:38.383Z"
            },
            {  

               "_id":"5d8a0dc446f10d679ab43888",
               "valid":true,
               "client":{firstname: 'something', lastname: 'something else'},
               "visit_time":"2019-09-24T12:34:23.210Z"
            },

         ]
      }
   ],
   "created_at":"2019-09-24T12:08:51.928Z",
   "__v":2
}

1 个答案:

答案 0 :(得分:1)

您可以使用以下聚合管道。

使用$ lookup填充代理,然后使用$ reduce和$ concatArrays收集所有客户端ID,然后使用$ lookup来获取客户端详细信息。

带有$ map的$ addFields可以迭代任务数组,并且对于每个客户端,通过按客户端ID和$ mergeObjects查找以保留其他字段,可以映射上一阶段的客户端信息。在$ project阶段删除多余的字段。

Mongo db 3.6及更高版本

Model.aggregate([
 {"$lookup":{
  "from":"agents",
  "localField":"agent",
  "foreignField":"_id",
  "as":"agent"
 }},
 {"$addFields":{"agent":{"$arrayElemAt":["$agent",0]}}},
 {"$addFields":{
   "client_ids":{
     "$reduce":{
       "input":"$missions",
       "initialValue":[],
       "in": {"$concatArrays":["$$value","$$this.clients.client"]}
     }
   }
 }},
 {"$lookup":{
   "from":"clients",
   "localField":"client_ids",
   "foreignField":"_id",
   "as":"client_info"
 }},
 {"$addFields":{
   "missions":{
     "$map":{
       "input":"$missions",
       "in":{
         "$mergeObjects":[
           "$$this",
           {"clients":{"$map":{
             "input":"$$this.clients",
             "in":{"$mergeObjects":[
               "$$this",
              {"client":{"$arrayElemAt":[
                "$client_info",
                {"$indexOfArray":["$client_ids","$$this._id"]}
              ]}}
            ]}
           }}}
         ]
       }
     }
   }
 }},
 {"$project":{"client_ids":0,"client_info":0}}
])

Mongo db小于3.6

$ lookup填充代理,然后$ unwind到达客户端并查找以获取客户端详细信息。使用$ group倒带以返回具有填充值的原始结构。

Model.aggregate([
 {"$lookup":{
  "from":"agents",
  "localField":"agent",
  "foreignField":"_id",
  "as":"agent"
 }},
 {"$addFields":{"agent":{"$arrayElemAt":["$agent",0]}}},
 {"$unwind":"$missions"},
 {"$unwind":"$missions.clients"},
 {"$lookup":{
   "from":"clients",
   "localField":"missions.clients.client",
   "foreignField":"_id",
   "as":"missions.clients.client"
 }},
 {"$addFields":{"missions.clients.client":{"$arrayElemAt":["$missions.clients.client",0]}}},
 {"$group":{
   "_id":{"_id":"$_id","mission_id":"$missions._id"},
   "agent":{"$first":"$agent"},
   "title":{"$first":"$missions.title"},
   "clients":{"$push":"$missions.clients"}
 }},
 {"$group":{
   "_id":"$_id._id",
   "agent":{"$first":"$agent"},
   "missions":{
     "$push":{
       "_id":"$_id.mission_id",
       "title":"$title",
       "clients":"$clients"
      }
    }
 }}
])