如何使用group by从mongodb中的数组对象中获取计数日期? [英] how to get count date wise using group by from the array object in mongodb?

查看:62
本文介绍了如何使用group by从mongodb中的数组对象中获取计数日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 mongodb 在图表中显示每日计数.我是今天第一次在图表中生成过去 5 天的日期.在我的 mongodb 中,我每天都有对象存储.显示我现在只想使用 group by 来计算日期,但我不知道如何使用 group by.我写了一个查询,但这个查询返回所有对象并且不返回任何计数,所以任何人都知道如何做到这一点,请告诉我.我也有一个日期,所以我想使用这个日期开始计数等于或大于.

i wanna go show daily count show in the chart using mongodb. i am first generate today to last 5 days date in the chart. and in the my mongodb i have the object store daily to daily. show i now i want to get count date wise only using group by but i have no idea how can use group by. i have write one query but this query return all the object and does't return any count so any one have idea how can do that please tell me. i have one date also so i want to start count using this date to equal or greater than.

这是我的查询 =>

 app.get(prefix + '/GetAnalyticsByUserId', function (req, res, next) {
    var instaid = req.query.instaid;
    var lastdate = req.query.lastdate; // this is my date in the timestamp i want to using this equal or greater date and count 
    InstaAc.aggregate(
                { $match: { _id: ObjectId("595f6bcdeb3db12064f26336") } },
                { $unwind: '$History' },
                { $match: { 'History.Action': { $eq: "Comment" } } },
                { $match: { 'History.datetime': { $gte: "datetime" } } },
                { $group: { _id: '$_id', History: { $push: '$History' } } }).exec(function (err, data) {
                    if (err) {
                        console.log(err); res.send(err)
                    }
                    else {                            
                            console.log(data);
                            res.send(data);                           
                    }
                })
});

这是我在数据库中的数组 =>

this is my array in the db =>

{
  "_id" : ObjectId("578fa05a7391bb0d34bd3c28"),
  "Action" : "Comment",
  "datetime" : 1507549688000
},
{
   "_id" : ObjectId("578fa05a7391bb0d34bd3c28"),
    "Action" : "Comment",
    "datetime" : 1507553288000
}
{
    "_id" : ObjectId("578fa05a7391bb0d34bd3c30"),
    "Action" : "Comment",
    "datetime" : 1507466888000
}

我预期的 o/p =>

my expecated o/p =>

 {
  "_id" : ObjectId("578fa05a7391bb0d34bd3c28"),
  "Action" : "Comment",
  "datetime" : 1507549688000
  "count":2
},    
{
    "_id" : ObjectId("578fa05a7391bb0d34bd3c30"),
    "Action" : "Comment",
    "datetime" : 1507466888000
    "count":1
}

在这里,我刚刚发布了一些像这样的对象,更多的对象存储在数据库中,所以我想使用按日期分组的方式进行计数.如果有人知道该怎么做,请告诉我.

here above i have just posted some object like this more object is store in the db so i want to get count using group by date.any one know how can do that then please let me.

推荐答案

你可以试试下面的聚合.

You can try below aggregation.

考虑下面的记录

{
  "_id": ObjectId("59db7a4d4f40c83c7bcaba0d"),
  "History": [
    {
      "_id": ObjectId("578fa05a7391bb0d34bd3c28"),
      "Action": "Comment",
      "datetime": millis 
    },
    {
      "_id": ObjectId("578fa05a7391bb0d34bd3c21"),
      "Action": "Comment",
      "datetime": millis 
    },
    {
      "_id": ObjectId("578fa05a7391bb0d34bd3c22"),
      "Action": "Comment",
      "datetime": millis 
    },
    {
      "_id": ObjectId("578fa05a7391bb0d34bd3c22"),
      "Action": "Comment",
      "datetime": millis 
    }
  ]
}

聚合:

InstaAc.aggregate([
  {
    "$match": {
      "_id": ObjectId("59db7a4d4f40c83c7bcaba0d"),
      "History.datetime": {
        "$gte": millis 
      }
    }
  },
  {
    "$unwind": "$History"
  },
  {
    "$match": {
      "History.datetime": {
        "$gte": millis 
      }
    }
  },
  {
  "$addFields": {
     "History.datetime": {
       "$add": [
         new Date(0),
         "$History.datetime"
        ]
      }
    }
  },
  {
    "$group": {
      "_id": {
        "$dateToString": {
          "format": "%Y-%m-%d",
          "date": "$History.datetime"
        }
      },
      "count": {
        "$sum": 1
      },
      "History": {
        "$push": "$History"
      }
    }
  },
  {
    "$sort": {
      "_id": 1
    }
  },
  {
    "$limit": 1
  }
])

输出:

{
  "_id": "2016-07-26",
  "count": 2,
  "History": [
    {
      "_id": ObjectId("578fa05a7391bb0d34bd3c21"),
      "Action": "Comment",
      "datetime": millis 
    },
    {
      "_id": ObjectId("578fa05a7391bb0d34bd3c22"),
      "Action": "Comment",
      "datetime": millis 
    }
  ]
}

这篇关于如何使用group by从mongodb中的数组对象中获取计数日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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