【问题标题】:Pymongo $in + aggregate with regex not working in pymongoPymongo $in + 聚合与正则表达式在 pymongo 中不起作用
【发布时间】:2017-07-03 17:03:33
【问题描述】:

我在 Mongo shell 中运行以下查询:

db.coll.aggregate([ { "$match" : {   "_id":{"$in" : [/^4_.*/,/^3_.*/]}}},
                 { "$unwind" : "$rp"},
                 {"$group":{"_id": "$_id", "rp": { "$push":  "$rp" }}} , {"$limit":120}],{allowDiskUse:true})

它工作正常。但是当我在 pymongo 中尝试相同的操作时:

ids_list = [3,4]
ids_list = ["^" + str(c_id) + "_.*" for c_id in ids_list]

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

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

这不起作用。我是 Mongo 查询的新手。

【问题讨论】:

    标签: python regex mongodb-query aggregation-framework pymongo


    【解决方案1】:

    我更改了使用 re 模块编译每个元素的 for 循环,即

    ids_list = [re.compile("^" + str(c_id) + "_.*") for c_id in ids_list]
    

    它成功了:)

    【讨论】:

      猜你喜欢
      • 2013-11-20
      • 2020-12-26
      • 2015-10-28
      • 1970-01-01
      • 2016-11-10
      • 2014-06-06
      • 2020-10-26
      • 2017-08-12
      相关资源
      最近更新 更多