简体   繁体   English

索引与聚合管道进行排序

[英]Index vs Aggregation Pipeline for Sorting

I'm developing an application using MongoDB as its database, and for sorting data, I encountered an interesting argument from a colleague that index can be used instead of aggregation pipeline for getting sorted data. 我正在开发一个使用MongoDB作为数据库的应用程序,并且在对数据进行排序时,我遇到了一个来自同事的有趣争论,即可以使用索引而不是聚合管道来获取排序的数据。

I tried this and it actually works; 我尝试过,它实际上有效; using an index with specified field and direction does return sorted data when queried. 使用具有指定字段和方向的索引在查询时会返回排序后的数据。 When using aggregation pipeline, I also obtained the same result. 当使用聚合管道时,我也获得了相同的结果。

I have created an index with the following specification: 我创建了一个具有以下规范的索引:

index name: batch_deleted_a_desc

num: asc
marked: asc
value: desc

Using aggregation pipeline: 使用聚合管道:

> db.test.aggregate([{$match: {num:"3",marked:false}}, {$sort:{"value":-1}}])
{ "_id" : ObjectId("5d70b40ba7bebd3d7c135615"), "value" : 4, "marked" : false, "num" : "3" }
{ "_id" : ObjectId("5d70b414a7bebd3d7c135616"), "value" : 2, "marked" : false, "num" : "3" }
{ "_id" : ObjectId("5d70b3fea7bebd3d7c135614"), "value" : 1, "marked" : false, "num" : "3" }

Using index: 使用索引:

> db.test.find({num:"3",marked:false})
{ "_id" : ObjectId("5d70b40ba7bebd3d7c135615"), "value" : 4, "marked" : false, "num" : "3" }
{ "_id" : ObjectId("5d70b414a7bebd3d7c135616"), "value" : 2, "marked" : false, "num" : "3" }
{ "_id" : ObjectId("5d70b3fea7bebd3d7c135614"), "value" : 1, "marked" : false, "num" : "3" }

As you can see, the results are the same. 如您所见,结果是相同的。 But I am unsure that using index for getting sorted data is a good practice, and yet using aggregation pipeline is (sometimes) taking more effort than just creating index. 但是我不确定使用索引来获取排序数据是一种好习惯,但是(有时)使用聚合管道比创建索引要花更多的精力。

So, which would be the best option? 那么,哪个是最佳选择?

In the context of the question, the better option would be the aggregation because it explicitly specifies the sort . 在问题的上下文中,更好的选择是聚合,因为它明确指定了sort

In the query example, results are being returned in order specified by the index because the query is using the index { num: 1, marked: 1, value: 1} . 在查询示例中,因为查询使用的是索引{ num: 1, marked: 1, value: 1}索引指定的顺序返回结果。 However, nothing specified in the query will guarantee that ordering, meaning results may change at some point in the future. 但是,查询中指定的任何内容都不能保证排序,这意味着结果将来可能会有所变化。 For example, consider the case where the index { num: 1, marked: 1, updated_at: 1 } were to be created. 例如,考虑要创建索引{ num: 1, marked: 1, updated_at: 1 }情况。 The query planner may decide to use this index instead, which may result in results in a different order. 查询计划者可能决定改用该索引,这可能导致结果以不同的顺序出现。

In the absence of a sort, a query would return results in the order of the index being used, but you should not rely upon that ordering without explicitly specifying it. 在没有排序的情况下,查询将按所使用索引的顺序返回结果,但是如果未明确指定索引,则不应依赖该顺序。 Quoting the docs : 引用文档

Unless you specify the sort() method or use the $near operator, MongoDB does not guarantee the order of query results. 除非您指定sort()方法或使用$ near运算符,否则MongoDB不保证查询结果的顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM