How to sort mongodb with pymongo



  • 3 Answer(s)

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

    If you need to sortby, let’s say, id then you should .sort(“_id”, 1)

    For many fields:

    .sort([("field1", pymongo.ASCENDING), ("field2", pymongo.DESCENDING)])
    
    Answered on January 3, 2019.
    Add Comment

    Just you can try to do this one:

    db.Account.find().sort("UserName")
    db.Account.find().sort("UserName",pymongo.ASCENDING)
    db.Account.find().sort("UserName",pymongo.DESCENDING)
    
    Answered on January 3, 2019.
    Add Comment

    It’s very simple, You can do it:

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

     

    Answered on January 3, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.