MongoDB Search allows you to configure sorting in your index to pre-sort documents in ascending or descending order at index-time instead of sorting at query-time. This is helpful when result sets are very large, making query-time sorting slow. You can use the sort index option with the following MongoDB Search field types:
booleandatenumber(valores enteros, de punto flotante y double)objectIdtokenuuid
You can also configure how you want null or missing values to be sorted using the noData option.
Sintaxis
The sort option has the following syntax:
1 { 2 "mappings": { 3 "dynamic": true|false, 4 "fields": { 5 "<field-name>": { 6 "type": "boolean" | "date" | "number" | "objectId" | "token" | "uuid" 7 } 8 } 9 }, 10 "sort": { "<field-to-sort>": 1 | -1 } | { "<field-to-sort>": { "order": 1 | -1, "noData": "lowest" | "highest" } } 11 }
Requisitos
To use a sorted index, you must:
Configure the fields used for
sortusing static mappings.booleandatenumber(valores enteros, de punto flotante y double)objectIdtoken(stringvalues)uuid
MongoDB Search supports fields that you index as multiple types for
sortif at least one of the types is one of the supported types listed above.To sort on nested fields, configure all intermediate fields up to the root field as the
documenttype.
Nota
If you specify the sort option with fields that are indexed using dynamic mappings or typeSet, the index build will fail.
Comportamiento
A sorted index physically orders MongoDB Search documents during index builds using the configured top-level sort instead of sorting at query time. This improves performance of queries whose $search.sort specification matches the index sort or a prefix of it, especially when sorting over very large result sets (such as more than one million documents in the results).
Use the explain output to verify whether a query uses the index sort. Queries that benefit from the index sort include the field usesIndexSort: true. A query benefits when its sort fields match the index sort or from a prefix of the index sort.
Nota
You can't use the searchScore meta expression as part of the sorted index because score is only computed at query time.
Soporte de arreglos
You can sort on array fields. MongoDB Search flattens the array and uses one representative value for comparison. For ascending sort, it uses the lowest value in the array and for a descending sort, it uses the highest value in the array.
For mixed-type arrays, the sort order is based on the MongoDB BSON Comparison Order. For an ascending sort, MongoDB Search uses the element with the lowest BSON type. For a descending sort, MongoDB Search uses the element with the highest BSON type.
Embedded Documents Support
You can sort on child fields only if all the parent documents are statically defined as the document type.
Queries using returnScope don't benefit from sorted index because the sorted index definition doesn't support embeddedDocuments field type in the index for sorting.
Considerations
Indexes with
sortspecifications might experience slower index builds and replication.Queries with
$search.sortspecified as the reverse of the index sort might be slower compared to using a non-sorted index. Queries using other$search.sortspecifications won't be impacted.MongoDB Search supports only one
sortspecification per index. If you want to sort on different fields, choose the dominant sort order as the index sort.
Ejemplos
The following examples create a sorted index on the fields in the sample_airbnb.listingsAndReviews collection.
The following index definition sorts the number_of_reviews and the last_reviewed fields in descending order.
{ "mappings": { "dynamic": false, "fields": { "number_of_reviews": { "type": "number" }, "last_reviewed": { "type": "date" } } }, "sort": { "number_of_reviews": -1, "last_reviewed": -1 } }
The following index definition sorts the number_of_reviews and the last_reviewed fields in descending order. It places documents with null or missing values at the bottom of the results.
{ "mappings": { "dynamic": false, "fields": { "number_of_reviews": { "type": "number" }, "last_reviewed": { "type": "date" } } }, "sort": { "number_of_reviews": { "order": -1, "noData": "lowest" }, "last_reviewed": { "order": -1, "noData": "lowest" } } }
The following index definition sorts on the date field in descending order. To sort on date field, it defines the reviews field as the embeddedDocuments type and the document type.
{ "mappings": { "dynamic": false, "fields": { "reviews": [ { "type": "embeddedDocuments", "dynamic": true }, { "type": "document", "dynamic": false, "fields": { "date": { "type": "date" } } } ] } }, "sort": { "reviews.date": -1 } }
The following index definition sorts on the property_type field in ascending order with null and missing values appearing at the bottom of the results. The property_type field is defined as the string, autocomplete, and token types. The index uses only the token type for sorting.
{ "mappings": { "dynamic": false, "fields": { "property_type": [ { "type": "string" }, { "type": "autocomplete", "tokenization": "edgeGram", "minGrams": 2, "maxGrams": 15 }, { "type": "token" } ] } }, "sort": { "property_type": { "order": 1, "noData": "highest" } } }