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?