$sort
$sort
The
$sort
modifier orders the elements of an array during a$push
operation.To use the
$sort
modifier, it must appear with the$each
modifier. You can pass an empty array[]
to the$each
modifier such that only the$sort
modifier has an effect.{ $push: { <field>: { $each: [ <value1>, <value2>, ... ], $sort: <sort specification> } } } For
<sort specification>
:To sort array elements that are not documents, or if the array elements are documents, to sort by the whole documents, specify
1
for ascending or-1
for descending.If the array elements are documents, to sort by a field in the documents, specify a sort document with the field and the direction, i.e.
{ field: 1 }
or{ field: -1 }
. Do not reference the containing array field in the sort specification (e.g.{ "arrayField.field": 1 }
is incorrect).
행동
MongoDB 5.0부터 업데이트 연산자는 문자열 기반 이름이 있는 문서 필드를 사전순으로 처리합니다. 숫자 이름이 있는 필드는 숫자 순서대로 처리됩니다. 자세한 내용은 업데이트 운영자 동작을 참조하십시오.
The $sort
modifier can sort array elements that are not
documents. In previous versions, the $sort
modifier required
the array elements be documents.
If the array elements are documents, the modifier can sort by either
the whole document or by a specific field in the documents. In previous
versions, the $sort
modifier can only sort by a specific
field in the documents.
Trying to use the $sort
modifier without the $each
modifier results in an error. The $sort
no longer requires
the $slice
modifier. For a list of modifiers available for
$push
, see Modifiers.
예시
Sort Array of Documents by a Field in the Documents
students
컬렉션을 생성합니다.
db.students.insertOne( { "_id": 1, "quizzes": [ { "id" : 1, "score" : 6 }, { "id" : 2, "score" : 9 } ] } )
The following update appends additional documents to the quizzes
array and then sorts all the elements of the array by the ascending
score
field:
db.students.updateOne( { _id: 1 }, { $push: { quizzes: { $each: [ { id: 3, score: 8 }, { id: 4, score: 7 }, { id: 5, score: 6 } ], $sort: { score: 1 } } } } )
중요
The sort document refers directly to the field in the
documents and does not reference the containing array field
quizzes
; i.e. { score: 1 }
and not { "quizzes.score": 1}
After the update, the array elements are in order of ascending
score
field:
{ "_id" : 1, "quizzes" : [ { "id" : 1, "score" : 6 }, { "id" : 5, "score" : 6 }, { "id" : 4, "score" : 7 }, { "id" : 3, "score" : 8 }, { "id" : 2, "score" : 9 } ] }
Sort Array Elements That Are Not Documents
다음 문서를 students
컬렉션에 추가합니다:
db.students.insertOne( { "_id" : 2, "tests" : [ 89, 70, 89, 50 ] } )
The following operation adds two more elements to the tests
array
and sorts the elements:
db.students.updateOne( { _id: 2 }, { $push: { tests: { $each: [ 40, 60 ], $sort: 1 } } } )
The updated document has the elements of the tests
array in
ascending order:
{ "_id" : 2, "tests" : [ 40, 50, 60, 70, 89, 89 ] }
Update Array Using Sort Only
다음 문서를 students
컬렉션에 추가합니다:
db.students.insertOne( { "_id" : 3, "tests" : [ 89, 70, 100, 20 ] } )
To update the tests
field to sort its elements in descending
order, specify the { $sort: -1 }
and specify an empty array []
for the $each
modifier. For example:
db.students.updateOne( { _id: 3 }, { $push: { tests: { $each: [ ], $sort: -1 } } } )
The example sorts the tests
field values in descending order:
{ "_id" : 3, "tests" : [ 100, 89, 70, 20 ] }
$sort
을(를) 다른 $push
수정자와 함께 사용
다음 문서를 students
컬렉션에 추가합니다:
db.students.insertOne( { "_id" : 5, "quizzes" : [ { "wk": 1, "score" : 10 }, { "wk": 2, "score" : 8 }, { "wk": 3, "score" : 5 }, { "wk": 4, "score" : 6 } ] } )
다음 $push
작업에서는 다음을 사용합니다.
quizzes
배열에 여러 문서를 추가하는
$each
수정자,
the
$sort
modifier to sort all the elements of the modifiedquizzes
array by thescore
field in descending order, andquizzes
배열의 처음 3개의 정렬된 요소만 유지하기 위해 사용하는
$slice
수정자.
db.students.updateOne( { _id: 5 }, { $push: { quizzes: { $each: [ { wk: 5, score: 8 }, { wk: 6, score: 7 }, { wk: 7, score: 6 } ], $sort: { score: -1 }, $slice: 3 } } } )
작업 후에는 가장 높은 점수를 받은 세 개의 퀴즈만 배열에 표시됩니다:
{ "_id" : 5, "quizzes" : [ { "wk" : 1, "score" : 10 }, { "wk" : 2, "score" : 8 }, { "wk" : 5, "score" : 8 } ] }
The order of the modifiers in the query does not change the order that the modifiers are applied. For details, see Modifiers.