如何使用mongodb进行多重操作并获得计数? [英] how to work with multiple action and get count using mongodb?

查看:91
本文介绍了如何使用mongodb进行多重操作并获得计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我为图表中的显示计数日期创建了一个查询.而且我用过mongodb.我有一个对象,并且在该对象中存储多个数据,并用一个值 Action 分开.我想显示日期明智的动作,如评论,就像两个动作都想要显示.现在,我只是显示评论数.我也想得到计数. 在下面,我列出了我的对象和查询.

here i have create one query for the show count date wise in the chart. and i have used mongodb. and i have one object and in the object store multiple data and separate with one value Action. i want to show count date wise Action like Comment,like both action count want show. right now i am just show comment count. i want also get like count. here below i have listed my object and query.

这是我的对象数组=>

here this is my array of the object =>

{
   "_id" : ObjectId("578fa05a7391bb0d34bd3c28"),
   "Action" : "Comment",
   "datetime" : 1507099928000 // 4th oct 2017 convert date just for info here write
},
{
  "_id" : ObjectId("578fa05a7391bb0d34bd3c28"),
  "Action" : "Comment",
   "datetime" : 1507099928000  // 4th oct 2017 convert date just for info here write
}
{
   "_id" : ObjectId("578fa05a7391bb0d34bd3c30"),
    "Action" : "Comment",
   "datetime" : 1507186328000 // 5th oct 2017 convert date just for info here write
 }
 {
  "_id" : ObjectId("578fa05a7391bb0d34bd3c28"),
  "Action" : "Comment",
  "datetime" : 1507193528000 // 5th oct 2017 convert date just for info here write
 },
 {
  "_id" : ObjectId("578fa05a7391bb0d34bd3c28"),
   "Action" : "Comment",
  "datetime" : 1507279928000 // 6th oct 2017 convert date just for info here write
 }
 {
   "_id" : ObjectId("578fa05a7391bb0d34bd3c30"),
   "Action" : "Comment",
   "datetime" : 1507020728000 // 3th oct 2017 convert date just for info here write
 }
 {
  "_id" : ObjectId("578fa05a7391bb0d34bd3c28"),
  "Action" : "Comment",
  "datetime" : 1507279928000  // 6th oct 2017 convert date just for info here write
 },
 {
   "_id" : ObjectId("578fa05a7391bb0d34bd3c28"),
   "Action" : "like",
   "datetime" : 1507279928000  // 6th oct 2017 convert date just for info here write
 }

我当前的o/p =>

my current o/p =>

[ { _id: '03-10-2017', count: 1 },
  { _id: '04-10-2017', count: 2 },
  { _id: '05-10-2017', count: 2 },
  { _id: '06-10-2017', count: 2 } ]

我除外o/p =>

[ { _id: '03-10-2017', count: 1 ,likecount:0},
  { _id: '04-10-2017', count: 2 ,likecount:0},
  { _id: '05-10-2017', count: 2,likecount:0 },
  { _id: '06-10-2017', count: 2,liekcount:1},     

这是我的查询=>

InstaAc.aggregate([
                        {
                            "$match": {
                                "_id": ObjectId("595e3b2033961713940442cf"),
                                "History.datetime": {
                               "$lte": 1507529239000, "$gte": 1507012285558
                                }
                            }
                        },
                        {
                            "$unwind": "$History"
                        },
                        {
                            "$match": {
                                "History.datetime": {
                                  "$lte": 1507529239000, "$gte": 1507012285558
                                },
                                "History.Action": {
                                    $eq: "comment"
                                }
                            }
                        },
                        {
                            "$addFields": {
                                "History.datetime": {
                                    "$add": [new Date(0), "$History.datetime"]
                                }
                            }
                        },
                            {
                                "$group": {
                                    "_id": {
                                        "$dateToString": {
                                            "format": "%d-%m-%Y",
                                            "date": "$History.datetime"
                                        }
                                    },
                                    "count": {
                                        "$sum": 1
                                    }                                    
                                }
                            },                           
                            {
                                "$sort": {
                                    "_id": 1
                                }
                            }
               ]).exec(function (err, data) {
if (err) {
    console.log(err); 
}
else {
    console.log(data);        
}
 })

任何人该怎么做,那么请告诉我.

any one how can do that then please let me know.

推荐答案

先将$match更新为

{
  "$match": {
    "_id": ObjectId("595e3b2033961713940442cf"),
    "History.datetime": {
      "$lte": 1507529239000,
      "$gte": 1507012285558
    },
    "History.Action": {
      "$in": [
        "Comment",
        "like"
      ]
    }
  }
}

和第二个$match

{
  "$match": {
    "History.datetime": {
      "$lte": 1507529239000,
      "$gte": 1507012285558
    },
    "History.Action": {
      "$in": [
        "Comment",
        "like"
      ]
    }
  }
}

$group

{
  "likecount": {
    "$sum": {
      "$cond": [
        {
          "$eq": [
            "$History.Action",
            "like"
          ]
        },
        1,
        0
      ]
    }
  }
}

这篇关于如何使用mongodb进行多重操作并获得计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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