【问题标题】:Find a subdocument in array PyMongo在数组 PyMongo 中查找子文档
【发布时间】:2020-08-09 09:16:08
【问题描述】:

我想查询任何用户在“2020-03-15”和“2020-04-25”之间对机器学习书籍制作了哪些 cmets,将 cmets 从最近到最近排序。

这是我的文件。

lib_books = db.lib_books
document_book1 = ({
    "bookid" : "99051fe9-6a9c-46c2-b949-38ef78858dd0",
    "title" : "Machine learning",
    "author" : "Tom Michael",
    "date_of_first_publication" : "2000-10-02",
    "number_of_pages" : 414,
    "publisher" : "New York : McGraw-Hill",
    "topics" : ["Machine learning", "Computer algorithms"],
    "checkout_list" : [
    {
        "time_checked_out" : "2020-03-20 09:11:22",
        "userid" : "ef1234",
        "comments" : [
        {
            "comment1" : "I just finished it and it is worth learning!",
            "time_commented" : "2020-04-01 10:35:13"
        },
        {
            "comment2" : "Some cases are a little bit outdated.",
            "time_commented" : "2020-03-25 13:19:13"
        },
        {
            "comment3" : "Can't wait to learning it!!!",
            "time_commented" : "2020-03-21 08:21:42"
        }]
    },
    {
        "time_checked_out" : "2020-03-04 16:18:02",
        "userid" : "ab1234",
        "comments" : [
        {
            "comment1" : "The book is a little bit difficult but worth reading.",
            "time_commented" : "2020-03-20 12:18:02"
        },
        {
            "comment2" : "It's hard and takes a lot of time to understand",
            "time_commented" : "2020-03-15 11:22:42"
        },
        {
            "comment3" : "I just start reading, the principle of model is well explained.",
            "time_commented" : "2020-03-05 09:11:42"
        }]
    }]
})

我试过这段代码,但它什么也没返回。

query_test = lib_books.find({"bookid": "99051fe9-6a9c-46c2-b949-38ef78858dd0", "checkout_list.comments.time_commented" : {"$gte" : "2020-03-20", "$lte" : "2020-04-20"}})
for x in query_test:
    print(x)

【问题讨论】:

    标签: python mongodb pymongo


    【解决方案1】:

    你可以试试这个

    pipeline = [{'$match':{'bookid':"99051fe9-6a9c-46c2-b949-38ef78858dd0"}},//bookid filter
                {'$unwind':'$checkout_list'},
                {'$unwind':'$checkout_list.comments'},
                {'$match':{'checkout_list.comments.time_commented':{"$gte" : "2020-03-20", "$lte" : "2020-04-20"}}},
                {'$project':{'_id':0,'bookid':1,'title':1,'comment':'$checkout_list.comments'}},
                {'$sort':{'checkout_list.comments.time_commented':-1}}]
    
    query_test = lib_books.aggregate(pipeline)
    #{"bookid": "99051fe9-6a9c-46c2-b949-38ef78858dd0", "checkout_list.comments.time_commented" : {"$gte" : "2020-03-20", "$lte" : "2020-04-20"}})
    for x in query_test:
        print(x)
    

    我建议您将评论字段保留为一个名称,而不是将其保留为“comment1”、“comment2”等。如果该字段为“comment”,则可以将其带到根目录

    聚合可以修改如下

    pipeline = [{'$match':{'bookid':"99051fe9-6a9c-46c2-b949-38ef78858dd0"}},//bookid filter
                {'$unwind':'$checkout_list'},
                {'$unwind':'$checkout_list.comments'},
                {'$match':{'checkout_list.comments.time_commented':{"$gte" : "2020-03-20", "$lte" : "2020-04-20"}}},
                {'$project':{'_id':0,'bookid':1,'title':1,'comment':'$checkout_list.comments.comment','time_commented':'$checkout_list.comments.time_commented'}},
                {'$sort':{'time_commented':-1}}]
    

    MongoDB 查询,以备不时之需

    db.books.aggregate([
    {$match:{'bookid':"99051fe9-6a9c-46c2-b949-38ef78858dd0"}},//bookid filter
    {$unwind:'$checkout_list'},
    {$unwind:'$checkout_list.comments'},
    {$match:{'checkout_list.comments.time_commented':{"$gte" : "2020-03-20", "$lte" : "2020-04-20"}}},
    {$project:{_id:0,bookid:1,title:1,comment:'$checkout_list.comments.comment',time_commented:'$checkout_list.comments.time_commented'}},
    {$sort:{'time_commented':-1}}
    ])
    

    如果需要搜索多个文档,则可以使用$in条件。

    {$match:{'bookid':{$in:["99051fe9-6a9c-46c2-b949-38ef78858dd0","99051fe9-6a9c-46c2-b949-38ef78858dd1"]}}},//bookid filter
    

    【讨论】:

    • 嗨阿比舍克,它有效!太感谢了!你能告诉我一些学习 PyMongo 或 MongoDB 聚合查询的资源吗?
    • 另外,如果书籍文档很多,我想过滤 bookid 字段怎么办?谢谢!
    • @QinZ,你可以了解更多关于MongoDB聚合的信息here我想我错过了上面聚合语句中的bookid过滤器。让我编辑我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多