问题
For mongoengine, there are operators all
and icontains
. But how can I combine the usage of them?
Say for each item, I just want to match with icontains
but not exact
? I tried to use regex. I works but unfortunately it would break if I have more than 1 Q
in the queryset of mongoengine. Because it would try to deepcopy
the pattern
object but unfortunately pattern
object can't been copied deeply.
回答1:
actually, I recommend using pymongo, install by easy_install pymongo
, in pymongo, you could try:
db.collections.find({'$and':[
{'field A':re.compile('your pattern')},
{'$ne':{'field A':'not exact word'}}
]})
here, $ne
and $and
are native mongo db operators, you could find more details mongodb advnace query
来源:https://stackoverflow.com/questions/11287001/how-to-combine-the-usage-of-operator-all-and-icontains-for-mongoengine