如何使用 pymongo 对 mongodb 进行排序
我尝试在查询 mongoDB 时使用排序功能,但失败了。相同的查询在 MongoDB 控制台中有效,但在这里不行。代码如下:
import pymongo
from pymongo import Connection
connection = Connection()
db = connection.myDB
print db.posts.count()
for post in db.posts.find({}, {'entities.user_mentions.screen_name':1}).sort({u'entities.user_mentions.screen_name':1}):
print post
我得到的错误如下:
Traceback (most recent call last):
File "find_ow.py", line 7, in <module>
for post in db.posts.find({}, {'entities.user_mentions.screen_name':1}).sort({'entities.user_mentions.screen_name':1},1):
File "/Library/Python/2.6/site-packages/pymongo-2.0.1-py2.6-macosx-10.6-universal.egg/pymongo/cursor.py", line 430, in sort
File "/Library/Python/2.6/site-packages/pymongo-2.0.1-py2.6-macosx-10.6-universal.egg/pymongo/helpers.py", line 67, in _index_document
TypeError: first item in each key pair must be a string
我在其他地方找到了一个链接,说如果使用 pymongo,我需要在密钥前面放置一个“u”,但这也不起作用。其他人可以让它工作或者这是一个错误。
I'm trying to use the sort feature when querying my mongoDB, but it is failing. The same query works in the MongoDB console but not here. Code is as follows:
import pymongo
from pymongo import Connection
connection = Connection()
db = connection.myDB
print db.posts.count()
for post in db.posts.find({}, {'entities.user_mentions.screen_name':1}).sort({u'entities.user_mentions.screen_name':1}):
print post
The error I get is as follows:
Traceback (most recent call last):
File "find_ow.py", line 7, in <module>
for post in db.posts.find({}, {'entities.user_mentions.screen_name':1}).sort({'entities.user_mentions.screen_name':1},1):
File "/Library/Python/2.6/site-packages/pymongo-2.0.1-py2.6-macosx-10.6-universal.egg/pymongo/cursor.py", line 430, in sort
File "/Library/Python/2.6/site-packages/pymongo-2.0.1-py2.6-macosx-10.6-universal.egg/pymongo/helpers.py", line 67, in _index_document
TypeError: first item in each key pair must be a string
I found a link elsewhere that says I need to place a 'u' infront of the key if using pymongo, but that didn't work either. Anyone else get this to work or is this a bug.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
pymongo 中的
.sort()
以key
和direction
作为参数。因此,如果您想按
id
排序,那么您应该.sort("_id", 1)
对于多个字段:
.sort()
, in pymongo, takeskey
anddirection
as parameters.So if you want to sort by, let's say,
id
then you should.sort("_id", 1)
For multiple fields:
你可以试试这个:
You can try this:
这也有效:
我在我的代码中使用它,如果我在这里做错了什么,请发表评论,谢谢。
This also works:
I'm using this in my code, please comment if i'm doing something wrong here, thanks.
为什么Python使用元组列表而不是字典?
在Python中,你不能保证字典会按照你声明的顺序解释。
因此,在 mongo shell 中,您可以执行
.sort({'field1':1,'field2':1})
,解释器将在第一级对 field1 进行排序,在第二级对 field 2 进行排序。如果在 python 中使用此语法,则有可能在第一级按 field2 排序。使用元组,就不存在这样的风险。
Why python uses list of tuples instead dict?
In python, you cannot guarantee that the dictionary will be interpreted in the order you declared.
So, in mongo shell you could do
.sort({'field1':1,'field2':1})
and the interpreter would sort field1 at first level and field 2 at second level.If this syntax was used in python, there is a chance of sorting by field2 at first level. With tuple, there is no such risk.
按
_id
降序排序:按
_id
升序排序:Sort by
_id
descending:Sort by
_id
ascending:描述与升序:
###################
DESC & ASC :
###################
TLDR:与传统的
.find().sort()
相比,聚合管道更快。现在开始真正的解释。在 MongoDB 中执行排序操作有两种方法:
.find()
和.sort()
。正如许多人所建议的 .find().sort() 是执行排序的最简单方法。
然而,与聚合管道相比,这是一个缓慢的过程。
来到聚合管道方法。实现用于排序的简单聚合管道的步骤是:
注意:根据我的经验,聚合管道的工作速度比
.find().sort()< /code> 方法。
这是聚合管道的示例。
自己尝试这个方法,比较速度并在评论中让我知道这一点。
编辑:在对多个字段进行排序时不要忘记使用
allowDiskUse=True
否则会抛出错误。TLDR: Aggregation pipeline is faster as compared to conventional
.find().sort()
.Now moving to the real explanation. There are two ways to perform sorting operations in MongoDB:
.find()
and.sort()
.As suggested by many .find().sort() is the simplest way to perform the sorting.
However, this is a slow process compared to the aggregation pipeline.
Coming to the aggregation pipeline method. The steps to implement simple aggregation pipeline intended for sorting are:
NOTE: In my experience, the aggregation pipeline works a bit faster than the
.find().sort()
method.Here's an example of the aggregation pipeline.
Try this method yourself, compare the speed and let me know about this in the comments.
Edit: Do not forget to use
allowDiskUse=True
while sorting on multiple fields otherwise it will throw an error.Python 使用键、方向。可以用上面的方法。
所以在你的情况下你可以这样做
Python uses key,direction. You can use the above way.
So in your case you can do this
假设您想按“created_on”字段排序,那么您可以这样做,
Say, you want to sort by 'created_on' field, then you can do like this,