How to sort mongodb with pymongo

前端 未结 7 1854
情书的邮戳
情书的邮戳 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: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
    

提交回复
热议问题