Node.js Mongo DB对象数组在另一个集合上查找
所以问题是,我的Posts集合的每个文档(comments字段)中都有一个对象数组。每个对象都有一个属性“commentAuthorId”。我不想做的事情是对comments数组中的每个对象执行查找,并根据“commentAuthorId”前面提到的内容将有关CommentAuthor的信息插入到该对象中。用户信息位于另一个名为“用户”的集合中。 这是我到目前为止尝试过的,但它不起作用Node.js Mongo DB对象数组在另一个集合上查找,node.js,database,mongodb,express,Node.js,Database,Mongodb,Express,所以问题是,我的Posts集合的每个文档(comments字段)中都有一个对象数组。每个对象都有一个属性“commentAuthorId”。我不想做的事情是对comments数组中的每个对象执行查找,并根据“commentAuthorId”前面提到的内容将有关CommentAuthor的信息插入到该对象中。用户信息位于另一个名为“用户”的集合中。 这是我到目前为止尝试过的,但它不起作用 { $unwind: '$comments' }, { $lo
{
$unwind: '$comments'
},
{
$lookup: {
from: "Users",
localField: "comments.commentAuthorId",
foreignField: "_id",
as: "commentAuthorDetails",
},
},
{$group: {
_id: "$_id",
comments: {
$push: {
_id: "$comments._id",
authorDetails: "$commentAuthorDetails"
}
}
}
文件示例-帖子:
{
"_id":{
"$oid":"5eb2ed9f4c53d52b8c69f01b"
},
"createdBy":"5e45bf41e2465801801455d9",
"postContent":"dasdsa\n#lazyloadingtest\n ",
"tags":[
"lazyloadingtest"
],
"addedAt":{
"$date":"2020-05-06T17:02:23.138Z"
},
"likes":[
],
"likesCount":0,
"comments":[
{
"content":"afdsfsdf",
"author":"Kacper",
"postId":"5eb2ed9f4c53d52b8c69f01b",
"commentAuthorId":"5e45bf41e2465801801455d9",
"addedAt":{
"$date":"2020-05-06T17:02:25.167Z"
},
"_id":{
"$oid":"5eb2eda14c53d52b8c69f01c"
}
}
]
}
文档示例-用户:
{
"_id":{
"$oid":"5e45bf41e2465801801455d9"
},
"name":"Kacper",
"email":"kacper@kacper.pl",
"password":"$2b$12$QeCw0AKlRI1kjAJc6eM5hOvxc5gRk/FhuI9vDcGLitsrSkHQZp8ky",
"userPicture":null
}
预期的查找聚合结果:
{
"_id":{
"$oid":"5eb2ed9f4c53d52b8c69f01b"
},
"createdBy":"5e45bf41e2465801801455d9",
"postContent":"dasdsa\n#lazyloadingtest\n ",
"tags":[
"lazyloadingtest"
],
"addedAt":{
"$date":"2020-05-06T17:02:23.138Z"
},
"likes":[
],
"likesCount":0,
"comments":[
{
"content":"afdsfsdf",
"author":"Kacper",
"postId":"5eb2ed9f4c53d52b8c69f01b",
"commentAuthorId":"5e45bf41e2465801801455d9",
"addedAt":{
"$date":"2020-05-06T17:02:25.167Z"
},
"_id":{
"$oid":"5eb2eda14c53d52b8c69f01c"
},
"postAuthorInfo": {
"_id":{
"$oid":"5e45bf41e2465801801455d9"
},
"name":"Kacper",
"email":"kacper@kacper.pl",
"password":"$2b$12$QeCw0AKlRI1kjAJc6eM5hOvxc5gRk/FhuI9vDcGLitsrSkHQZp8ky",
"userPicture":null
}
}
]
}
只有一条评论,但我不想在“commentAuthorId”字段中查找下一条评论。请共享示例文档。和所需输出。我将用示例文档和所需输出更新问题。请共享示例文档。和所需的输出。我将用示例文档和所需的输出更新问题。