Node.js 是否按每个父id筛选嵌入的文档?
我有文件Node.js 是否按每个父id筛选嵌入的文档?,node.js,mongodb,mongoose,mongodb-query,aggregation-framework,Node.js,Mongodb,Mongoose,Mongodb Query,Aggregation Framework,我有文件 { "_id" : ObjectId("5da832caeb173112348e509b"), "owner" : { "image" : "5d999578aeb073247de4bd6e.jpg", "fullname" : "hem sopheap", "userID" : "5d999578aeb073247de4bd6e" }, "project" : {}, "image" : "hem
{
"_id" : ObjectId("5da832caeb173112348e509b"),
"owner" : {
"image" : "5d999578aeb073247de4bd6e.jpg",
"fullname" : "hem sopheap",
"userID" : "5d999578aeb073247de4bd6e"
},
"project" : {},
"image" : "hem sopheap-1571304138866.png",
"body" : "Lorem Ipsum "),
"comments" : [
{
"user" : "5d999578aeb073247de4bd6e",
"fullname" : "hem sopheap",
"username" : "sopheap",
"comment" : "1000000",
"_id" : ObjectId("5db07900ae100b0c05b1222c"),
"replies" : [],
"date" : ISODate("2019-10-23T15:40:57.535Z"),
"likes" : [
"5da85558886aee13e4e7f044",
"5da85558886aee13e4e7f044",
"5da85558886aee13e4e7f044",
"5da85558886aee13e4e7f044",
"5da85558886aee13e4e7f044",
"5da85558886aee13e4e7f044",
"5da85558886aee13e4e7f044",
"5da85558886aee13e4e7f044",
"5da85558886aee13e4e7f044",
"5da85558886aee13e4e7f044",
"5da85558886aee13e4e7f044"
]
},
{
"user" : "5d999578aeb073247de4bd6e",
"fullname" : "hem sopheap",
"username" : "sopheap",
"comment" : "11111111111",
"_id" : ObjectId("5db0790aae100b0c05b1222d"),
"replies" : [],
"date" : ISODate("2019-10-23T15:40:57.535Z"),
"likes" : []
}
],
"__v" : 33,
"likes" : [
"5d999578aeb073247de4bd6e"
]
}
我如何获取喜欢的,通过post\u id
和评论\u id
进行过滤,以获得结果喜欢的
"likes" : [
"5da85558886aee13e4e7f044",
"5da85558886aee13e4e7f044",
"5da85558886aee13e4e7f044",
"5da85558886aee13e4e7f044",
"5da85558886aee13e4e7f044",
"5da85558886aee13e4e7f044",
"5da85558886aee13e4e7f044",
"5da85558886aee13e4e7f044",
"5da85558886aee13e4e7f044",
"5da85558886aee13e4e7f044",
"5da85558886aee13e4e7f044"
]
让我们简化对这个的输入。我们有两个帖子,P0
和P2
。每个都有一个数组注释
。我们知道这条评论。_id至少在帖子中是唯一的,所以可以在这里“重用”它们:
var r =
[
{
"_id" : "P0",
"comments" : [
{ "_id" : "C0", "likes" : [ "AA", "AA" ] }
,{"_id" : "C1", "likes" : [] }
,{"_id" : "C2", "likes" : [ "foo", "bar" ]}
,{"_id" : "C3", "likes" : []
}
]
}
,{
"_id" : "P2",
"comments" : [
{ "_id" : "C0", "likes" : [ "FF", "FF" ] }
,{"_id" : "C1", "likes" : [] }
,{"_id" : "C2", "likes" : [ "foo", "bar" ]}
,{"_id" : "C3", "likes" : []
}
]
}
];
以下是一个解决方案:
db.foo.aggregate([
// First, match on post ID and comments ID. Remember, comments is an
// array so ANY comments entry with key C0 inside the array will match and
// yield the entire array. But this is OK because it very much narrows down
// the info to process:
{$match: {_id: "P2", "comments._id":"C0"}}
,{$unwind: "$comments"} // Unwind the comments:
// And now pick only that comment with ID C0:
,{$match: {"comments._id":"C0"}}
// To complete the request, make "likes" a top level field:
,{$project: {"likes": "$comments.likes"}}
]);
OP的完整答案将包括设置post和注释id为ObjectId
而不是字符串,但查询是相同的。清楚:所以你说的是给定的“\u id”:ObjectId(“5da832caeb173112348e509b”)
和(使用点路径)注释。\u id:ObjectId(“5DB07900AE100C05B11222C”)
作为查询条件,只返回likes
数组?如果是的话,comments.\u id
在帖子中是唯一的吗?我想是的,但是…你能帮我提供完整的答案吗?是的,但是我想弄清楚注释数组是如何设置的。如果一篇文章可以包含多个具有相同评论id的评论,那么答案就会改变。评论id在一篇文章中是否总是唯一的?一篇文章有许多评论,评论。\ id是唯一的。是的,它在这里起作用: