首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否按每个父_id过滤嵌入的文档?

是否按每个父_id过滤嵌入的文档?
EN

Stack Overflow用户
提问于 2019-10-27 21:20:40
回答 1查看 52关注 0票数 0

我有文档

代码语言:javascript
复制
{
    "_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"
    ]
}

如何获取likes、按帖子_id和评论_id进行过滤以获得结果likes

代码语言:javascript
复制
"likes" : [ 
                    "5da85558886aee13e4e7f044", 
                    "5da85558886aee13e4e7f044", 
                    "5da85558886aee13e4e7f044", 
                    "5da85558886aee13e4e7f044", 
                    "5da85558886aee13e4e7f044", 
                    "5da85558886aee13e4e7f044", 
                    "5da85558886aee13e4e7f044", 
                    "5da85558886aee13e4e7f044", 
                    "5da85558886aee13e4e7f044", 
                    "5da85558886aee13e4e7f044", 
                    "5da85558886aee13e4e7f044"
                ]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-28 00:16:05

让我们将输入简化为这个。我们有两个帖子,P0P2。每个都有一个数组comments。我们知道comment._id至少在文章中是独一无二的,所以在这里“重用”它们是可以的:

代码语言:javascript
复制
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" : []
        }
    ]
 }
];

这里有一个解决方案:

代码语言:javascript
复制
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和comment _id设置为ObjectId而不是字符串,但查询是相同的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58579776

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档