简体   繁体   English

Pymongo 聚合 $in 列表

[英]Pymongo aggregate $in list

I'm trying to get some specific documents in my collection.我试图在我的收藏中获取一些特定的文档。 I want documents that have a substring in one filed of my database (display_url) and that also look for some key words that must have in another field (edge_media_to_caption.edges.node.text).我想要在我的数据库的一个字段 (display_url) 中有一个子字符串的文档,并且还查找在另一个字段 (edge_media_to_caption.edges.node.text) 中必须具有的一些关键字。 The first field is an url so I need to use wildcard, the only way that seems to work is using this signal: .*第一个字段是一个 url,所以我需要使用通配符,唯一可行的方法是使用这个信号:.*

However I'm having problems with the second part of my match where I use $in I think it is not working.但是,我在使用 $in 的比赛的第二部分遇到问题,我认为它不起作用。 This second field is a string field with text>第二个字段是一个带有 text> 的字符串字段

So I need to get documents that have a regex expression that i give (I tested this part alone and is working) and that also have at least one of the words ['.因此,我需要获取包含我给出的正则表达式的文档(我单独测试了这部分并且正在工作)并且至少包含 ['. corona.电晕。 ','. ','。 virus.病毒。 ','. ','。 vírus.病毒。 ','. ','。 covid.冠状病毒病。 ','. ','。 pandemia.大流行。 ','. ','。 pândemia.瘟疫。 '] in the text. '] 在文中。

        client = MongoClient('localhost', 27017)
        db = client.basededados
        collection = getattr(db, pdados) 
        pipeline= [{'$project': {"_id": True,
                          'legenda': '$edge_media_to_caption.edges.node.text',
                          'data': '$taken_at_timestamp',
                          'hash': '$tags',
                          'id' :'$display_url'}},
            {'$match': {'$and': [{"id": {"$regex": '/%s/' % nitem[0]}},
                                 {"legenda": {"$in": ['.*corona.*','.*virus.*','.*vírus.*','.*covid.*','.*pandemia.*','.*pândemia.*']}}
                                ]}}
                    ]

To wildcard match a string, use a regex .要通配符匹配字符串,请使用正则表达式 In pure Mongo:在纯 Mongo 中:

{$in: [/\.corona\./, ...]}

In pymongo, you can use native Python regexen:在 pymongo 中,您可以使用原生 Python 正则表达式:

import re

...

{'$in': [re.compile(r'\.corona\.'), ...]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM