是否可以在Aggregation Frameworks mongo中按投影顺序获取字段 [英] Is it possible to get the fields in the order of projection in Aggregation Frameworks mongo

查看:107
本文介绍了是否可以在Aggregation Frameworks mongo中按投影顺序获取字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下文件:

{ "_id" : 3, "quizzes" : [ 4, 5, 5 ], "labs" : [ 6, 5 ], "final" : 78, "midterm" : 70 }
{ "_id" : 1, "quizzes" : [ 4, 5, 5 ], "labs" : [ 6, 5 ], "midterm" : 70 }

如果我运行以下查询:

   db.students.aggregate([  { "$project": {  "midterm": 1,"final": 1   } } ])

结果如下:

{ "_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 }

用例说明订单为何重要:

我有一个集合,可以为用户存储每日数据.如果用户当天进行了某些活动,则该用户的文档中将有一个字段.我们将这一天存储200天...在另一个集合中我们必须更新过去200天内userFirstActive的时间...

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...

因此,如果我们进行200天的预测...除id以外的第一个条目将是活动的第一天.是否有可能在没有获取整个文档并检查密钥是否存在的情况下实现这一目标? /p>

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?

推荐答案

默认情况下,MongoDB返回字段的插入顺序.

MongoDB by default return fields in order of their insertion.

例如

db.students.aggregate([  { "$project": {  "midterm": 1,"final": 1   } } ])

将返回

{ "_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.

例如

db.students.aggregate([  { "$project": {  _midterm:"$midterm","_final": "$final"}}])

以上查询将返回

{ "_id" : 3, "_midterm" : 70, "_final" : 78 }
{ "_id" : 2, "_midterm" : 60, "_final" : 55 }
{ "_id" : 1, "_midterm" : 70 }

这里,期中是第一位,而最后是第二位,只有一个例外.字段名称以_为前缀.如果您想使用原始名称,则可以再次project.

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"}}
])

它将返回

{ "_id" : 3, "midterm" : 70, "final" : 78 }
{ "_id" : 2, "midterm" : 60, "final" : 55 }
{ "_id" : 1, "midterm" : 70 }

这篇关于是否可以在Aggregation Frameworks mongo中按投影顺序获取字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆