如何查找对象数组,并且每个对象的每个对象都有不同的ObjectId

时间:2019-05-02 11:07:09

标签: mongodb

我在somecollection中有一组配置文件。配置文件数组看起来像

profiles:[
{userProfile : ObjectId("5cb9588b6f74784babadd38b")},
{customerProfile : ObjectId("5cb9588b6f74784babadd40b")},
{clientProfile : ObjectId("5cb9588b6f74784babadd78b")},
{adminProfile : ObjectId("5cb9588b6f74784babadd66b")}
]

现在,我想查看每个概要文件集合,并在单个概要文件数组中获取每个概要文件的数据。

My Desired Output is : 

profiles:[
{userProfile : 
{data of user profile}
 },

{customerProfile : {data of user customerProfile}},

{clientProfile : {data of user clientProfile}},

{adminProfile : {data of user adminProfile}}
]

我尝试的解决方案是查找4次,结果不是我期望的。     代码下方

db.getCollection('clients').aggregate([
{                           $lookup            
                                from: 'userProfile ',
                                localField: 'userProfile ',
                                foreignField: '_id',
                                as: 'userProfile '
                            }

},

{                           $lookup            
                                from: 'customerProfile ',
                                localField: 'customerProfile ',
                                foreignField: '_id',
                                as: 'customerProfile '
                            }

},

{                           $lookup            
                                from: 'clientProfile ',
                                localField: 'clientProfile ',
                                foreignField: '_id',
                                as: 'clientProfile '
                            }

},

{                           $lookup            
                                from: 'adminProfile ',
                                localField: 'adminProfile ',
                                foreignField: '_id',
                                as: 'adminProfile '
                            }

}                          
])

1 个答案:

答案 0 :(得分:0)

假设您使用的mongo版本低于3.6,并且想要继续使用此查找语法,则需要先展开数组:

db.getCollection('clients').aggregate([
                                  { $unwind: "$profiles"},
                                    {                           
                                    $lookup: {           
                                    from: 'userProfile ',
                                    localField: 'profiles.userProfile ',
                                    foreignField: '_id',
                                    as: 'userProfile '
                                  }
                            },
                            {                          
                                    $lookup:{            
                                    from: 'customerProfile ',
                                    localField: 'profiles.customerProfile ',
                                    foreignField: '_id',
                                    as: 'customerProfile '
                                   }
                            },
                         {  
                                  $lookup:{          
                                    from: 'clientProfile ',
                                    localField: 'profiles.clientProfile ',
                                    foreignField: '_id',
                                    as: 'clientProfile '
                                }
                           },
                           {     
                                $lookup:{        
                                    from: 'adminProfile ',
                                    localField: 'profiles.adminProfile',
                                    foreignField: '_id',
                                    as: 'adminProfile '
                                }
                            }                          
])

但是Mongo 3.6+版具有一种查询语法,可以使您完成所有这些操作而无需展开数组并保持原始结构:

单次查找将如下所示:

{
   $lookup:
     {
       from: adminProfile,
       let: { profile_ids: "$profiles.adminProfile" },
       pipeline: [ 
          { $match:
             { $expr:
                { $in: [ "$_id",  "$$profile_ids" ] },
             }
          }],
       as: admins
     }
}