【问题标题】:Mongo expression with variable input具有可变输入的 Mongo 表达式
【发布时间】:2016-10-25 04:37:33
【问题描述】:

我正在尝试编写一个可以满足两种情况的 mongo 搜索。一个字段等于“False”,另一个字段可以是任何值。

我想出了以下几点:

  if p:
     # supposed to be my wild card expression
     mongoExp={"$eq": {"$or":[{False},{"$ne":False}}}
  else:
     # where field is equal to False
     mongoExp={"$eq": False}

  cursor=self.zel.find({"xxx": mongoExp}).sort("field1", pymongo.DESCENDING)

但是这不起作用如何使用 $eq mongo 运算符进行通配符搜索?

【问题讨论】:

    标签: mongodb pymongo motorengine


    【解决方案1】:

    你可以试试这个:

    if p:
         # supposed to be my wild card expression
         mongoExp={"$or":[{"$eq":False},{"$ne":False}]}
      else:
         # where field is equal to False
         mongoExp={"$eq": False}
    
      cursor=self.zel.find({"xxx": mongoExp}).sort("field1", pymongo.DESCENDING)
    

    【讨论】:

      猜你喜欢
      • 2011-06-28
      • 1970-01-01
      • 2020-09-02
      • 2022-09-23
      • 1970-01-01
      • 2021-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多