Is it possible to get the fields in the order of p

2019-01-25 13:37发布

问题:

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?

回答1:

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 }