$ lookup有多个集合

时间:2018-12-14 12:54:18

标签: mongodb mongodb-query aggregation-framework

我在这里有4个收藏集:用户工作表项目计费

我想从工作表集合中获取数据 和工作表集合中的数据如下:

/ 1 /
{    "_id" : ObjectId("5c0bb867135ec3a669f52e04"),
    "isActive" : true,
    "projectId" : ObjectId("5bff9d5549a40e64a5f57040"),
    "hours" : 8,
    "billingId" : ObjectId("5bff995f3bf7ebd47f78cc7c"),
    "taskDetails" : "Work on schedule page.\nWork on add schedule page.",
    "userId" : ObjectId("5be6cbf8b844bb8e48a5839f")
}
/ 2 /
{    "_id" : ObjectId("5c0def8c737bad2e6064fec6"),
    "isActive" : true,
    "projectId" : ObjectId("5bff9d5549a40e64a5f57040"),
    "hours" : 6,
    "taskDetails" : "Attendance Page\nProject Page",
    "userId" : ObjectId("5be6cbf8b844bb8e48a5839f")
}
/ 3 /
{    "_id" : ObjectId("5c0def8c737bad2e6064fec7"),
    "isActive" : false,
    "projectId" : ObjectId("5bff9ca949a40e64a5f5703f"),
    "hours" : 2,
    "taskDetails" : "Review and test site.",
    "userId" : ObjectId("5be6cbf8b844bb8e48a5839f")
}
/ 4 /
{    "_id" : ObjectId("5c138cedafa36016041aa5a2"),
    "isActive" : true,
    "projectId" : ObjectId("5bff9ca949a40e64a5f5703f"),
    "hours" : 3,
    "taskDetails" : "this is task description",
    "userId" : ObjectId("5be6cbf8b844bb8e48a5839f")
}
/ 5 /
{    "_id" : ObjectId("5c138cedafa36016041aa5a3"),
    "isActive" : false,
    "projectId" : ObjectId("5bff9d5549a40e64a5f57040"),
    "hours" : 5,
    "billingId" : ObjectId("5bff995f3bf7ebd47f78cc7c"),
    "taskDetails" : "LN PM project",
    "userId" : ObjectId("5be6cbf8b844bb8e48a5839f")
}

我想要以以下格式显示结果:

[
 {
   _id : "as worksheet id",
   userId : "userId",
   userName : "populate userId and get user name",
   projects : [
     {
       projectId : "as projectId"
       projectName : "populate projectId and get projectName",
       hours : "as per projectid from worksheet collection",
       isActive : "as per projectid from worksheet collection",
       billingId : "populate billingId and get data"
     },
     {
       projectId : "as projectId"
       projectName : "populate projectId and get projectName",
       hours : "as per projectid from worksheet collection",
       isActive : "as per projectid from worksheet collection",
       billingId : "populate billingId and get data"
     },
   ]
 }
]

因此,基本上,我想获取一个用户的工作表,其中包含该用户的所有项目,并包含我在上面的代码中提到的工作表集合中的小时数和其他字段。

0 个答案:

没有答案