This article continues to explore updating the document array type field. You can think about whether you encounter such needs in daily life. After the data is inserted into the array field, the array fields need to be sorted. For example, find out the cities with the highest temperature yesterday, or the cities with the most precipitation. Or the students with the highest grades. Sorting is required here. In the $push operation, Mongodb provides the $sort data modifier, allowing users to sort the array after inserting elements into the array.
definition
The $sort method modifies the sorting of array elements during the $push operation. The $sort method must be used together with $each. mongodb allows users to pass an empty array to the $each method, ensuring that users can sort the array without inserting elements into the array. The $sort method is used in the following form.
{ $push: { <field>: { $each: [<value1>, <value2>, ...], $sort: <sort specification> } } }
For sort specification, when sorting non-document arrays, or sorting the entire document as a whole, the positive order can be specified 1, and the reverse order can be specified as -1. When sorting document fields, specify the fields and order of sorting to be sorted. No need to add the field name of the array field.
Behavior
- Since mongodb5.0, the UPDATE operation updates fields in dictionary order of field names. When a field contains numbers, the fields are updated in order of numbers. Of course, operations on multiple fields of a document are atomic.
- $sort can sort documents in an array. This sorting can occur throughout the document or in part of the document.
- The $sort method must be used together with the $each method, otherwise an error will be reported.
application
Sort documents in arrays
Insert data into the students collection, where quzzes is the document array type field.
( { _id:1, quzzes: [ {id:1, score:6}, {id:2, score:9} ] } )
Build an update statement, requiring array elements to be inserted into quzzes and sorted in score positive order.
( {_id:1}, { $push: { quzzes: { $each: [ {id:3, score:8}, {id:4, score:7}, {id:5, score:6} ], $sort: {score:1} } } } )
View data update results
().pretty(); [ { "_id": 1, "quzzes": [ { "id": 1, "score": 6 }, { "id": 5, "score": 6 }, { "id": 4, "score": 7 }, { "id": 3, "score": 8 }, { "id": 2, "score": 9 } ] } ]
Use $sort to arrange non-document data types.
Insert data into the students collection. Where the test field is an array of numeric types.
({ _id:2, tests: [ 89,70,89, 50 ] })
Update the inserted data, requiring the insertion of new data 40, 60 and sorting the array in positive order.
({ _id:2 },{ $push: { tests: { $each: [40, 60], $sort: 1 } } })
View data update results
() { "_id": 2, "tests": [ 40, 50, 60, 70, 89, 89 ] }
Sorting arrays with only $sort
Insert data into the students collection, where tests is an array of numeric types
({ _id:3, tests: [89, 70, 100, 20] })
Modify the newly inserted document and require the tests field to be sorted in reverse order.
({ _id:3 },{ $push: { tests: { $each: [], $sort: -1 } } })
View updated results
{
"_id": 3,
"tests": [
100,
89,
70,
20
]
}
The above is the detailed content of the sample code of Mongodb UPDATE using $sort to reorder arrays. For more information about reordering Mongodb $sort arrays, please pay attention to my other related articles!