在PyMongo中使用.sort [英] Using .sort with PyMongo

查看:290
本文介绍了在PyMongo中使用.sort的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用PyMongo,当我尝试检索按其数字"和日期"字段排序的对象时,如下所示:

With PyMongo, when I try to retrieve objects sorted by their 'number' and 'date' fields like this:

db.test.find({"number": {"$gt": 1}}).sort({"number": 1, "date": -1})

我收到此错误:

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

我的排序查询出了什么问题?

What's wrong with my sort query?

推荐答案

sort应该是键方向对的列表,即

sort should be a list of key-direction pairs, that is

db.test.find({"number": {"$gt": 1}}).sort([("number", 1), ("date", -1)])

之所以必须是列表,是因为参数的顺序很重要,而dict在Python中没有顺序. 3.6

The reason why this has to be a list is that the ordering of the arguments matters and dicts are not ordered in Python < 3.6

这篇关于在PyMongo中使用.sort的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆