MongoDB 索引中的空值如何排序? [英] How are null values in a MongoDB index sorted?
本文介绍了MongoDB 索引中的空值如何排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想以特定方式 (-1, 1) 对索引进行排序,并希望确保空值位于索引的顶部.我如何确保这一点?
I want to sort my index in a specific way (-1, 1) and want to make sure that null values are at the top of the index. How can I ensure this?
推荐答案
如果您按降序排序并且在末尾看到 null
值,这将是排序的默认行为.
If you are sorting descending and you are seeing null
values at the end, that would be the default behaviour of the sort.
实际上没有太多可以改变这种行为的方法,但是可以为您提供所需结果的解决方法是执行两个查询而不是一个:
There's really not much that can be done to change that behaviour, but a workaround that will give you the results you're looking for is to do two queries instead of one:
db.Collection.find( { a: null } );
db.Collection.find( { a: { $ne: null } } ).sort( { a: -1, b: 1 } );
这篇关于MongoDB 索引中的空值如何排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文