Pymongo,空排序

Pymongo, empty sorting

如何使用默认的空参数调用 pymongo sort() 函数,这样我就不会进行排序。

collection.find(params).sort([])
ValueError: key_or_list must not be the empty list

collection.find(params).sort({})
TypeError: if no direction is specified, key_or_list must be an instance of list

collection.find(params).sort([{}])
ValueError: not enough values to unpack (expected 2, got 0)

在 mongo 控制台中我可以用

db.collection.find().sort({})

不能传递空列表进行排序。

以下 2 个查询 return 相同的结果

self.collection.find()
self.collection.find().sort([("$natural", pymongo.ASCENDING)])

因此,如果您希望忽略排序,只需将 [("$natural", pymongo.ASCENDING)] 传递给排序即可。

你可以参考这个answer