I have the following documents:
{ "_id" : 3, "quizzes" : [ 4, 5, 5 ], "labs" : [ 6, 5 ], "final" : 78, "midterm" : 70 }
{ "_id" : 1, "quizzes" : [ 4, 5, 5 ], "labs" : [ 6, 5 ], "midterm" : 70 }
If i run the below query:
db.students.aggregate([ { "$project": { "midterm": 1,"final": 1 } } ])
The result is as follows:
{ "_id" : 3, "final" : 78, "midterm" : 70 }
{ "_id" : 1, "midterm" : 70 }
If i change the order in projection still in the shell the fields are coming in the same order? can we reatain the order based on which its queried?
db.students.aggregate([ { "$project": { "final":1, "midterm": 1 } } ])
{ "_id" : 3, "final" : 78, "midterm" : 70 }
{ "_id" : 1, "midterm" : 70 }
Use case why order is important:
I have a collection which stores the day wise data for the user.If the user did some activity on that day there would an field in document for that user.We store this day for 200 days... In one more collection we have to update when is the userFirstActive in last 200 days...
Note: On the day user didnt perform any activity there wont be any entry for that key...
So if we make a projection for 200 days...the first entry other than id would be the first active day..Is it possible to acheive this without getting the whole document and check if the key present or not?
In MongoDb, the aggregation is assisted by several methods and operators that can perform different tasks. Among those operators, the $sort operator helps to sort the documents and return the documents in an organized order. And for group sorting in MongoDB, the $group operator is used with the $sort operator.
aggregate() method. Creates new documents in a sequence of documents where certain values in a field are missing. Returns literal documents from input values. Processes multiple aggregation pipelines within a single stage on the same set of input documents.
To sort the whole array by value, or to sort by array elements that are not documents, identify the input array and specify 1 for an ascending sort or -1 for descending sort in the sortBy parameter.
MongoDB offers an expansive list of operators that you can use across various aggregation stages. Each of these operators can be used to construct expressions for use in the aggregation pipeline stages. Operator expressions are similar to functions that take arguments.
in the last value of the aggregate array, add this property
{
$replaceRoot: {
newRoot: {
field_1: '$field_1',
field_2: '$field_2',
...
}
}
}
MongoDB by default return fields in order of their insertion.
e.g.
db.students.aggregate([ { "$project": { "midterm": 1,"final": 1 } } ])
will return
{ "_id" : 3, "final" : 78, "midterm" : 70 }
{ "_id" : 2, "midterm" : 60, "final" : 55 }
{ "_id" : 1, "midterm" : 70 }
as you can see second record, it's fields are in order of which we inserted. However, we can play a trick to get them in order you want by renaming fields.
e.g.
db.students.aggregate([ { "$project": { _midterm:"$midterm","_final": "$final"}}])
above query will return
{ "_id" : 3, "_midterm" : 70, "_final" : 78 }
{ "_id" : 2, "_midterm" : 60, "_final" : 55 }
{ "_id" : 1, "_midterm" : 70 }
here midterm is first and final is second with one exception. fields names are prefixed with _
. if you want original names, you can project
again.
db.students.aggregate(
[
{ "$project": { _midterm:"$midterm","_final": "$final"}},
{ "$project": { midterm:"$_midterm","final": "$_final"}}
])
and it will return
{ "_id" : 3, "midterm" : 70, "final" : 78 }
{ "_id" : 2, "midterm" : 60, "final" : 55 }
{ "_id" : 1, "midterm" : 70 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With