mongolDB聚合将相似文档分组在一起

时间:2015-12-14 21:51:53

标签: mongodb mongodb-query

我在mongoDB中有一个集合,每天都会有一个带有采样数据的文档。我想观察字段的变化。

我想使用mongoDB聚合将相似的项目分组到第一个:

+--+-------------------------+
|id|field             | date |
+--+-------------------------+
| 1|hello             | date1|
+--+-------------------------+
| 2|foobar            | date2|  \_   Condense these into one row with date2
+--+-------------------------+  /
| 3|foobar            | date3|
+--+-------------------------+
| 4|hello             | date4|
+--+-------------------------+
| 5|world             | date5|  \__   Condense these into a row with date5
+--+-------------------------+  /
| 6|world             | date6|
+--+-------------------------+
| 7|puppies           | date7|
+--+-------------------------+
| 8|kittens           | date8|  \__   Condense these into a row with date8
+--+-------------------------+  /
| 9|kittens           | date9|
+--+-------------------------+

是否可以为此问题创建mongoDB聚合?

以下是MySQL中类似问题的答案: Grouping similar rows next to each other in MySQL

样本数据

数据已按日期排序。

这些文件:

{ "_id" : "566ee064d56d02e854df756e", "date" : "2015-12-14T15:29:40.432Z", "score" : 59 },
{ "_id" : "566a8c70520d55771f2e9871", "date" : "2015-12-11T08:42:23.880Z", "score" : 60 },
{ "_id" : "566932f5572bd1720db7a4ef", "date" : "2015-12-10T08:08:21.514Z", "score" : 60 },
{ "_id" : "5667e652c021206f34e2c9e4", "date" : "2015-12-09T08:29:06.696Z", "score" : 60 },
{ "_id" : "5666a468cc45e9d9a82b81c9", "date" : "2015-12-08T09:35:35.837Z", "score" : 61 },
{ "_id" : "56653fe099799049b66dab97", "date" : "2015-12-07T08:14:24.494Z", "score" : 60 },
{ "_id" : "5663f6b3b7d0b00b74d9fdf9", "date" : "2015-12-06T08:49:55.299Z", "score" : 60 },
{ "_id" : "56629fb56099dfe31b0c72be", "date" : "2015-12-05T08:26:29.510Z", "score" : 60 }

应分组:

{ "_id" : "566ee064d56d02e854df756e", "date" : "2015-12-14T15:29:40.432Z", "score" : 59 }
{ "_id" : "566a8c70520d55771f2e9871", "date" : "2015-12-11T08:42:23.880Z", "score" : 60 }
{ "_id" : "5666a468cc45e9d9a82b81c9", "date" : "2015-12-08T09:35:35.837Z", "score" : 61 }
{ "_id" : "56653fe099799049b66dab97", "date" : "2015-12-07T08:14:24.494Z", "score" : 60 }

1 个答案:

答案 0 :(得分:1)

如果您不坚持使用aggregation框架,可以通过迭代光标并将每个文档与前一个文档进行比较来完成:

var cursor = db.test.find().sort({date:-1}).toArray();
var result = [];
result.push(cursor[0]); //first document must be saved
for(var i = 1; i < cursor.length; i++) {
    if (cursor[i].score != cursor[i-1].score) {
        result.push(cursor[i]);
    }
}

结果:

[
    {
        "_id" : "566ee064d56d02e854df756e",
        "date" : "2015-12-14T15:29:40.432Z",
        "score" : 59
    },
    {
        "_id" : "566a8c70520d55771f2e9871",
        "date" : "2015-12-11T08:42:23.880Z",
        "score" : 60
    },
    {
        "_id" : "5666a468cc45e9d9a82b81c9",
        "date" : "2015-12-08T09:35:35.837Z",
        "score" : 61
    },
    {
        "_id" : "56653fe099799049b66dab97",
        "date" : "2015-12-07T08:14:24.494Z",
        "score" : 60
    }
]