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

查看:16
本文介绍了是否可以在聚合框架 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 }

如果我仍然在 shell 中更改投影顺序,字段的顺序是否相同?我们可以保留其查询的顺序吗?

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 之外的第一个条目将是第一个活动日..是否有可能在不获取整个文档并检查密钥是否存在的情况下实现这一目标?

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 }

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

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