我正在尝试使用键 position
对名为 user_score
的集合进行排序,并获取结果的第一个文档。在这种情况下,集合 user_score
不存在,我希望得到结果为 None
,但我得到了一个光标。
1。 结果 =
db.user_score.find({'score':'$lt':score}}).sort("position,pymongo.DESCENDING").limit(1)
现在我像下面这样更改了我的查询,但没有得到任何预期的结果。
2。 结果 =
db.user_score.find_one({'score':{'$lt':score}}, sort=[("position", pymongo.DESCENDING)])
我的第一个查询有什么问题?
谢谢
最佳答案
我的回复有点晚了,但当前版本的 PyMongo 似乎确实支持 sort对 find_one 调用的操作。
来自文档页面 here (请用 grep 查找 find_one 部分):
All arguments to find() are also valid arguments for find_one(), although any limit argument will be ignored. Returns a single document, or None if no matching document is found.
示例用法如下:
filterdict = {'email' : 'this.is@me.com'}
collection.find_one(filterdict, sort=[('lastseen', 1)])
希望这对最近的搜索者有所帮助!
关于python - pymongo 排序和 find_one 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10079107/