-
Type:
Task
-
Resolution: Gone away
-
Priority:
Major - P3
-
None
-
Affects Version/s: None
-
Component/s: MongoDB 3.4
-
None
-
None
-
None
-
None
-
None
-
None
-
None
I want to return a list of posts with their owner object, a list of comments of each post that each comment brings to its owner object.
I try to do this with a nested $ lookup but realize that this is not possible:
postsdb.aggregate([
{
$lookup:
{
from: "users",
localField: "owner",
foreignField: "_id",
as: "user"
},
$lookup:
{
from: "comments",
localField: "comments",
foreignField: "_id",
as: "comments_list",
$lookup:
{
from: "users",
localField: "owner",
foreignField: "_id",
as: "user"
},
},
}
]).toArray(function(err, docs) {
console.log(err)
if (err) return res.status(500).err(err);
return res.json({posts: docs})
});
I want to get an object with such a structure:
posts: array [
0: {
title: '',
owner: {
userName: '',
profileImage: ''
},
comments: [
0: {
title: '',
owner: {
userName: '',
profileImage: ''
},
}]
},
1: {
title: '',
owner: {
userName: '',
profileImage: ''
},
comments: [
0: {
title: '',
owner: {
userName: '',
profileImage: ''
},
}]
},
]