如何使用 mongodb 处理多个操作并获取计数?

2024-05-24

在这里,我为图表中的显示计数日期创建了一个查询。我已经使用了mongodb。我有一个对象,在该对象中存储多个数据并用一个值分隔Action。我想显示计数日期明智的操作,例如评论,就像两个操作计数都想显示一样。现在我只是显示评论数。我也想得到喜欢的计数。 下面我列出了我的对象和查询。

这是我的对象数组=>

{
   "_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 =>

[ { _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);        
}
 })

任何人都可以如何做到这一点,请告诉我。


先更新$match to

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

第二个$match to

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

and $group to

{
  "likecount": {
    "$sum": {
      "$cond": [
        {
          "$eq": [
            "$History.Action",
            "like"
          ]
        },
        1,
        0
      ]
    }
  }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 mongodb 处理多个操作并获取计数? 的相关文章

随机推荐