假设我有一个关键字列表,我想与 MongoDB 文档进行匹配,其中它们都出现在 keys
字段(即 AND 子句)中。
如何在 Python 中做到这一点?
我已经尝试过:
keywords = ['a', 'b', 'c']
keywords_dict = {'keys' : keywords}
results = collection.find(keywords_dict)
但似乎没有返回结果。
我正在使用 Python3.5 和 PyMongo。
有什么提示吗?
最佳答案
您可以使用$all
运算符,也相当于 $and
指定值的运算;即以下语句:
{ 'keys': { '$all': ['a', 'b', 'c'] } }
相当于:
{ '$and': [ { 'key': a }, { 'key': b }, { 'key': c} ] }
关于python - 如何使用 Python 和 MongoDB 执行 $and 搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33657342/