mongodb查找聚合查询

时间:2017-03-03 21:09:54

标签: mongodb aggregation-framework lookup

我在mongodb中运行此查询时遇到问题。

db.user.aggregate([
      {
   $lookup:
     {
       from: 'role',
       localField: 'Role_id',
       foreignField: '_id',
       as: 'role'
     }
},
{
 $lookup:
     {
       from: 'location',
       localField: 'Location_id',
       foreignField: '_id',
       as: 'location'
     }   
 }
])

收藏品如下

//位置集

{
    "_id" : ObjectId("58b9b9b95565ef59ac1bcc99"),
    "locationcode" : "L001",
    "locationname" : "Pune"
}

//角色集合

{
    "_id" : ObjectId("58b9b8ef5565ef59ac1bcc49"),
    "rolename" : "SuperAdmin"
}

//用户集合:

{
    "_id" : ObjectId("58b9bad55565ef59ac1bcd04"),
    "LoginId" : "Sylvester_M001",
    "Password" : "123456",
    "Name" : "Sylvester",
    "EmployeeId" : "M001",
    "Role_id" : ObjectId("58b9b8ef5565ef59ac1bcc49"),
    "Location_id" : ObjectId("58b9b9b95565ef59ac1bcc99")
}

当我运行聚合查找以加入我得到的三个集合时

//结果集合:

{
    "_id" : ObjectId("58b9bad55565ef59ac1bcd04"),
    "LoginId" : "Sylvester_M001",
    "Password" : "123456",
    "Name" : "Sylvester",
    "EmployeeId" : "M001",
    "Role_id" : ObjectId("58b9b8ef5565ef59ac1bcc49"),
    "Location_id" : ObjectId("58b9b9b95565ef59ac1bcc99"),
    "role" : [ 
        {
            "_id" : ObjectId("58b9b8ef5565ef59ac1bcc49"),
            "rolename" : "SuperAdmin"
        }
    ],
    "location" : [ 
        {
            "_id" : ObjectId("58b9b9b95565ef59ac1bcc99"),
            "locationcode" : "L001",
            "locationname" : "Pune"
        }
    ]
}

但几秒钟后,它会变为

{
"$err":"Invalid ns[]",
"code":73
}

为什么会这样?关于我哪里出错的任何澄清?什么引发了这个?

0 个答案:

没有答案