如何根据mongodb中的日期对记录进行分组

时间:2017-05-11 13:47:40

标签: node.js mongodb

如何按日期分组数据?我在mongo中有一些看起来像这样的文档:是的,我已经添加了我的实际mongo doc和我的代码

    {
            "_id" : ObjectId("58c0e32161ccc654160b776a"),
            "consumer_id" : ObjectId("579f03069b49a0840409df83"),
            "user_id" : "579f034c9b49a0840409df85",
            "values" : [
                    {
                            "date" : "2017/2/9",
                            "point" : 1
                    },
                    {
                            "date" : "2017/2/10",
                            "point" : -1
                    },
                    {
                            "date" : "2017/2/11",
                            "point" : -1
                    }
            ]
    }
    {
            "_id" : ObjectId("58c0e3db61ccc654160b776b"),
            "consumer_id" : ObjectId("579f03069b49a0840409df83"),
            "user_id" : "579ef6f5a15b0eac1332034e",
            "values" : [
                    {
                            "date" : "2017/2/9",
                            "point" : 1
                    },
                    {
                            "date" : "2017/2/10",
                            "point" : 1
                    },
                    {
                            "date" : "2017/2/11",
                            "point" : -1
                    }
            ]
    }

我希望能够按日期计算点数 我的代码是这样的

var array = [];
    var array2 = [];
    db.coll.find({}).toArray(function(err, result) {
        result.map(function(data) {
            array.push(data.values)
        })

3 个答案:

答案 0 :(得分:0)

您需要使用mongodb中的sort函数:

sort({datefield: -1}}
例如 Mongodb 中的

db.products.find().sort({"created_at": 1}) --- 1 for asc and -1 for desc

nodejs 中,例如:

collection.find().sort({datefield: -1}, function(err, cursor){...});

答案 1 :(得分:0)

您需要执行map / reduce

我假设您的对象数组存储在文档的data字段中,它们本身存放在items集合中。

// map function which pushes the points in an array associated to the given date date
var mapDataPoints = function() {
  for (var i=0; i<this.data.length; i++) emit(this.data[i].date, this.data[i].points);
};

// reduce function, which sums the array of points for a given date
var reduceDataPoints = function(dateId, points) {
  return Array.sum(points);
};

// map-reduce operation over the collection, which takes each document and apply the map function, then run the reduce function on the result of the map operation
// result is stored in a new collection, called pointsByDate
db.items.mapReduce(
  mapDataPoints,
  reduceDataPoints,
  { out: "pointsByDate" }
)

// pointsByDate can be queried just like any collection
db.pointsByDate.find({ "_id": "2017/2/10" });

答案 2 :(得分:0)

  

以下代码适用于您的问题。

db.Stack.aggregate([
  {$unwind: "$values"},
  {$group: {
    _id: "$values.date",
    sum: {$sum: "$values.point"}
    }
  }
])
  

这是代码的输出

/ * 1 * / {     &#34; _id&#34; :&#34; 2017/2/11&#34;,     &#34;和&#34; :-2.0 }

/ * 2 * / {     &#34; _id&#34; :&#34; 2017/2/10&#34;,     &#34;和&#34; :0.0 }

/ * 3 * / {     &#34; _id&#34; :&#34; 2017/2/9&#34;,     &#34;和&#34; :2.0 }