在MongoDB$filter条件下使用python-re-regex

在MongoDB$filter条件下使用python-re-regex,regex,mongodb,aggregation-framework,Regex,Mongodb,Aggregation Framework,我有如下文件: { _id: 1, data: [ { zip: 001, city: "abc" }, { zip: 002, city: "xyz" } ] } 我想使用python正则表达式过滤数据数组。但它似乎不起作用 city = "abc" regx = re.compile("^%s$" %city, re.IGNORECASE|re.MULTILINE) for doc in db.testusers.aggregate([ { "$project": { "d

我有如下文件:

{
    _id: 1,
    data: [ { zip: 001, city: "abc" }, { zip: 002, city: "xyz" } ]
}
我想使用python正则表达式过滤数据数组。但它似乎不起作用

city = "abc"
regx = re.compile("^%s$" %city, re.IGNORECASE|re.MULTILINE)
for doc in db.testusers.aggregate([ { "$project": { "data": { "$filter": { "input": "$data", "as": "item", "cond": { "$eq": [ "$$item.city", regx ] } } } } } ]):
    json.dumps(doc)
它与任何东西都不匹配。

我做得对吗?

我认为$filter不支持正则表达式。看

我无法在此处测试此功能,但根据此示例,它应该可以正常工作:

city_list = ["cityAbc", "Metroid"]
city_list = [re.compile("^" + str(c_id) + "$", re.IGNORECASE) for c_id in city_list]

pipe = [ { "$match" : {   "_id":{"$in" : city_list}}},
                { "$unwind" : "$rp"},
                {"$group":{"_id": "$_id", "rp": { "$push":  "$rp" }}} , {"$limit":500}] 

res = list(db.coll.aggregate(pipeline = pipe,allowDiskUse=True))