简体   繁体   English

具有可变输入的Mongo表达式

[英]Mongo expression with variable input

I'm trying write a mongo search that can cater for two scenarios. 我正在尝试编写可满足两种情况的mongo搜索。 One where the field is equal to 'False' the other when the field can be any value. 一个字段等于“ False”,另一个字段可以是任何值。

I have come up with the following: 我想出了以下几点:

  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)

However this isn't working how can i get a wild card search with the $eq mongo operator? 但是,这不起作用,如何使用$ eq mongo运算符进行通配符搜索?

You could try this: 您可以尝试以下方法:

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)

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

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