如何使用 pymongo 对 mongodb 进行排序

发布于 2024-12-15 11:29:55 字数 999 浏览 3 评论 0原文

我尝试在查询 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

笑着哭最痛 2024-12-22 11:29:55

pymongo 中的 .sort()keydirection 作为参数。

因此,如果您想按 id 排序,那么您应该 .sort("_id", 1)

对于多个字段:

.sort([("field1", pymongo.ASCENDING), ("field2", pymongo.DESCENDING)])

.sort(), in pymongo, takes key and direction as parameters.

So if you want to sort by, let's say, id then you should .sort("_id", 1)

For multiple fields:

.sort([("field1", pymongo.ASCENDING), ("field2", pymongo.DESCENDING)])
栀梦 2024-12-22 11:29:55

你可以试试这个:

db.Account.find().sort("UserName")  
db.Account.find().sort("UserName",pymongo.ASCENDING)   
db.Account.find().sort("UserName",pymongo.DESCENDING)  

You can try this:

db.Account.find().sort("UserName")  
db.Account.find().sort("UserName",pymongo.ASCENDING)   
db.Account.find().sort("UserName",pymongo.DESCENDING)  
别忘他 2024-12-22 11:29:55

这也有效:

db.Account.find().sort('UserName', -1)
db.Account.find().sort('UserName', 1)

我在我的代码中使用它,如果我在这里做错了什么,请发表评论,谢谢。

This also works:

db.Account.find().sort('UserName', -1)
db.Account.find().sort('UserName', 1)

I'm using this in my code, please comment if i'm doing something wrong here, thanks.

过期情话 2024-12-22 11:29:55

为什么Python使用元组列表而不是字典?

在Python中,你不能保证字典会按照你声明的顺序解释。

因此,在 mongo shell 中,您可以执行 .sort({'field1':1,'field2':1}) ,解释器将在第一级对 field1 进行排序,在第二级对 field 2 进行排序。

如果在 python 中使用此语法,则有可能在第一级按 field2 排序。使用元组,就不存在这样的风险。

.sort([("field1",pymongo.ASCENDING), ("field2",pymongo.DESCENDING)])

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.

.sort([("field1",pymongo.ASCENDING), ("field2",pymongo.DESCENDING)])
苍风燃霜 2024-12-22 11:29:55

_id 降序排序:

collection.find(filter={"keyword": keyword}, sort=[( "_id", -1 )])

_id 升序排序:

collection.find(filter={"keyword": keyword}, sort=[( "_id", 1 )])

Sort by _id descending:

collection.find(filter={"keyword": keyword}, sort=[( "_id", -1 )])

Sort by _id ascending:

collection.find(filter={"keyword": keyword}, sort=[( "_id", 1 )])
无声静候 2024-12-22 11:29:55

描述与升序:

import pymongo

client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["mydatabase"]
col = db["customers"]

doc = col.find().sort("name", -1) #

for x in doc:
  print(x)

###################

import pymongo

client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["mydatabase"]
col = db["customers"]

doc = col.find().sort("name", 1) #

for x in doc:
  print(x)

DESC & ASC :

import pymongo

client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["mydatabase"]
col = db["customers"]

doc = col.find().sort("name", -1) #

for x in doc:
  print(x)

###################

import pymongo

client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["mydatabase"]
col = db["customers"]

doc = col.find().sort("name", 1) #

for x in doc:
  print(x)
何必那么矫情 2024-12-22 11:29:55

TLDR:与传统的.find().sort()相比,聚合管道更快。

现在开始真正的解释。在 MongoDB 中执行排序操作有两种方法:

  1. 使用 .find().sort()
  2. 或者使用聚合管道。

正如许多人所建议的 .find().sort() 是执行排序的最简单方法。

.sort([("field1",pymongo.ASCENDING), ("field2",pymongo.DESCENDING)])

然而,与聚合管道相比,这是一个缓慢的过程。

来到聚合管道方法。实现用于排序的简单聚合管道的步骤是:

  1. $match (可选步骤)
  2. $sort

注意:根据我的经验,聚合管道的工作速度比 .find().sort()< /code> 方法。

这是聚合管道的示例。

db.collection_name.aggregate([{
    "$match": {
        # your query - optional step
    }
},
{
    "$sort": {
        "field_1": pymongo.ASCENDING,
        "field_2": pymongo.DESCENDING,
        ....
    }
}])

自己尝试这个方法,比较速度并在评论中让我知道这一点。

编辑:在对多个字段进行排序时不要忘记使用 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:

  1. Using .find() and .sort().
  2. Or using the aggregation pipeline.

As suggested by many .find().sort() is the simplest way to perform the sorting.

.sort([("field1",pymongo.ASCENDING), ("field2",pymongo.DESCENDING)])

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:

  1. $match (optional step)
  2. $sort

NOTE: In my experience, the aggregation pipeline works a bit faster than the .find().sort() method.

Here's an example of the aggregation pipeline.

db.collection_name.aggregate([{
    "$match": {
        # your query - optional step
    }
},
{
    "$sort": {
        "field_1": pymongo.ASCENDING,
        "field_2": pymongo.DESCENDING,
        ....
    }
}])

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.

岛徒 2024-12-22 11:29:55
.sort([("field1",pymongo.ASCENDING), ("field2",pymongo.DESCENDING)])

Python 使用键、方向。可以用上面的方法。

所以在你的情况下你可以这样做

for post in db.posts.find().sort('entities.user_mentions.screen_name',pymongo.ASCENDING):
        print post
.sort([("field1",pymongo.ASCENDING), ("field2",pymongo.DESCENDING)])

Python uses key,direction. You can use the above way.

So in your case you can do this

for post in db.posts.find().sort('entities.user_mentions.screen_name',pymongo.ASCENDING):
        print post
一腔孤↑勇 2024-12-22 11:29:55

假设您想按“created_on”字段排序,那么您可以这样做,

.sort('{}'.format('created_on'), 1 if sort_type == 'asc' else -1)

Say, you want to sort by 'created_on' field, then you can do like this,

.sort('{}'.format('created_on'), 1 if sort_type == 'asc' else -1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文