【发布时间】:2018-05-15 14:40:19
【问题描述】:
这是我的方法:
def mongo_find(collection_name, find_value):
list(MongoClient("localhost:27017").db[collection_name].find(find_value))
find_value = {'milestones.content': {'$regex': 'The idea'}}, {'milestones.content'}
print(list(mongo_find(collection_name, find_value)))
我收到此错误:
TypeError: filter must be an instance of dict, bson.son.SON, or other type that inherits from collections.Mapping
但以下工作正常:
list(MongoClient("localhost:27017").db[collection_name].find({'milestones.content': {'$regex': 'The idea'}}, {'milestones.content'}))
所以在运行mongo_find 方法时我尝试打印:
print(find_value)
({'milestones.content': {'$regex': 'The idea'}}, {'milestones.content'})
可能是因为两端都加了圆括号。有解决办法吗?
【问题讨论】:
-
我不是蟒蛇人。不应该是项目 {'milestones.content':1}
-
嗨!是的,它将按升序排列。在我这样做的方式中,它会在没有对齐的情况下打印出来。谢谢
-
为什么 $regex 中没有模式我只看到纯文本。
-
在上面的代码中,“db”代表什么。你能试试 list(MongoClient("localhost:27017")[
].collection_name.find({'milestones.content': {'$regex': 'The idea'}}, {'milestones.content'} ))
标签: python-3.x mongodb arguments