pymongo问题集合

1. 问题:

 

使用sort排序,代码如下:

db.test.find().sort({"name" : 1, "age" : 1})  不能使用字典

遇到如下异常:

TypeError: if no direction is specified, key_or_list must be an instance of list  

   解决方法:

db.test.find().sort([("name", 1), ("age" , 1)])  使用列表+元组方式

原因:在python中只能使用列表进行排序,不能使用字典

https://blog.csdn.net/u010649766/article/details/78676953

sort用法

1作为函数调用使用

res = collection.find({"name":"login"}).sort([("time",-1)])

2 作为插入参数使用

collection = db_conn[collection_name]
result = collection.find( query_conditioin, projection={"_id":True, "dev_status":True, "created_at":True}, limit=3, sort=[("time",-1)] )
其中time是要分类的字段,-1为值(按照个人需要写)

posted @ 2019-08-18 13:51  Wsnan  阅读(285)  评论(0编辑  收藏  举报