Node.js 使用mongo,根据嵌套和子嵌套文档的过滤器选择一个字段,如Post->Comment->Likes
我想得到所有喜欢的具体评论,而不是喜欢的帖子 SQL语句中类似的内容:Node.js 使用mongo,根据嵌套和子嵌套文档的过滤器选择一个字段,如Post->Comment->Likes,node.js,mongodb,express,mongoose,mongodb-query,Node.js,Mongodb,Express,Mongoose,Mongodb Query,我想得到所有喜欢的具体评论,而不是喜欢的帖子 SQL语句中类似的内容: { "_id" : ObjectId("5da7c114753c6202f4f4d3b2"), "image" : "hem sopheap-1571275028119.jpg", "body" : "my first post\r\nyes this my post", "date" : ISODate("2019-10-17T00:59:18.901Z"), "comments"
{
"_id" : ObjectId("5da7c114753c6202f4f4d3b2"),
"image" : "hem sopheap-1571275028119.jpg",
"body" : "my first post\r\nyes this my post",
"date" : ISODate("2019-10-17T00:59:18.901Z"),
"comments" : [
{
"user" : "5d999578aeb073247de4bd6e",
"fullname" : "hem sopheap",
"username" : "sopheap",
"comment" : "My first comment",
"_id" : ObjectId("5da7c125753c6202f4f4d3b4"),
"replies" : []
},
{
"user" : "5da85558886aee13e4e7f044",
"fullname" : "soeng kanel",
"username" : "",
"comment" : "Mr. kansael comment",
"_id" : ObjectId("5da8557f886aee13e4e7f045"),
"replies" : [],
"date" : ISODate("2019-10-17T10:10:37.394Z"),
"likes" : [
"5d999578aeb073247de4bd6e",
"5d999578aeb073247de4bd6f"
]
}
],
"__v" : 41,
"likes" : [
"5d999578aeb073247de4bd6e",
"5d999578aeb073247de4bd6p"
]
}
而预期的结果应该是这样的
喜欢:[
5d999578aeb073247de4bd6e,
5d999578aeb073247de4bd6f
]
了解
这是完整的查询
db.posts.find{
评论:{
$elemMatch:{
用户:5da85558886aee13e4e7f044,
_id:Objected5DA8557F886AEE13E4E7F045
}
}
}, {
喜欢:$comments.likes,\u id:0
}.prettyI已更新查询,以便按照您的要求格式化输出:[id]。你得到了正确的结果吗?请注意,如果这有助于解决您的问题,请向上投票。如果这解决了问题,请接受答案。但是有很多喜欢的东西或元素,我只能从您的查询中得到一个喜欢的。可以帮助更多,获得喜欢下的元素列表,可以帮助检查我的示例输入数据。
db.posts.find({
"comments": {
$elemMatch: {
"_id": ObjectId("5da8557f886aee13e4e7f045")
}
}
}, {
likes:"$comments", _id:0
}).pretty()