How to sort mongodb with pymongo

前端 未结 7 1801
情书的邮戳
情书的邮戳 2020-11-28 04:01

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:



        
相关标签:
7条回答
  • 2020-11-28 04:27

    .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)])
    
    0 讨论(0)
  • 2020-11-28 04:31

    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.

    0 讨论(0)
  • 2020-11-28 04:37

    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)])
    
    0 讨论(0)
  • 2020-11-28 04:43

    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)
    
    0 讨论(0)
  • 2020-11-28 04:45

    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.

    0 讨论(0)
  • 2020-11-28 04:48
    .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
    
    0 讨论(0)
提交回复
热议问题