How to use lookup to an array of objects?

What not to do so, because the output is only one object with the user data and all
There is a collection of posts that have a field with comments, here is the document structure (schema, mongoose)
{
 url: { type: String },
 comments: [{
 owner: { type: mongoose.Schema.Types.ObjectId, ref: 'Users', required: !0 },
 post: { type: String, trim: !0, required: !0 },
 public: { type: Number, default: 0 },
 createdAt: { type: Date, default: Date.now }
}]
}


The goal is simple, to comments and for each owner to assign the data from the collection users, here is what I WANT to:
{
 comments: [{
 owner: {
 _id: 'User ID',
 avatar: the 'user avatar'
},
 post: 'text of comment',
 createdAt: 'here the creation date of the comment'
}]
}


Trying to do so
Posts.aggregate([
 { $match: { url: 'url url' } },
{
 $lookup: {
 from: 'users',
 localField: 'comments.owner',
 foreignField: '_id',
 as: 'comments.owner'
}
 }, {
 $project: {
 _id: 1,
 comments: 1
}
}
]).....

The output is not what you need
"comments":{ 
 "owner":[ 
 { 
"_id":"54ad4806f273e66f2bfa6984",
"username":"username",
"avatar":"54ad4806f273e66f2bfa6984.jpg",
"createdAt":"2013-06-16T22:11:37.000 Z"
}
]
}

How to build a query?
June 8th 19 at 20:23
0 answer

Find more questions by tags Node.jsMongooseMongoDB