【问题标题】:mongodb: Perform operation on data according to datemongodb:根据日期对数据进行操作
【发布时间】:2018-08-06 19:54:56
【问题描述】:

这是一个示例 JSON 对象,有 1000 个类似的对象,存储在我的 MongoDB 集合中。

{
  "_id": ObjectId("5b1bb74ffc7ee601c6915939"),
  "groupId": "-abcde",
  "applicationId": "avcvcvc",
  "integration": "web",
  "Category": "message",
  "Action": "message",
  "Type": "newMessage",
  "Id": "activity",
  "data": {
    "test": "good morning"
  },
  "timestamp": 1528543055858.0,
  "createdAt": ISODate("2018-06-09T11:17:35.868+0000"),
  "updatedAt": ISODate("2018-06-09T11:17:35.868+0000"),
  "__v": NumberInt(0)
}

这是我根据日期获取数据的查询

db.collection.find({"createdAt" : { $gte : new ISODate("2018-06-09T11:17:35.868+0000") }});

这是我需要对从获取数据 acc 中收到的 JSON 对象执行的操作。迄今为止

db.collection.aggregate( [
  { $match: { $or: [ { Type:"on mouse hover click" },{Type:"on mouse out"},
                     {Type : "on chat start"},{Type :"Load Event"}
                     ] } },
  { $group: { _id:null , count: { $sum: 1 } } }
] );

有什么方法可以让这两个操作在一个查询中执行,而不是获取数据。先约会,然后再进行聚合?我是 MongoDB 新手,所以我不知道该怎么做。

【问题讨论】:

    标签: javascript mongodb mongoose mongodb-query


    【解决方案1】:

    您可以只$match 获取聚合期间的时间戳:

     db.collection.aggregate( [
       { $match: { "createdAt" : { $gte : new ISODate("2018-06-09T11:17:35.868+0000") }}},
       { $match: { $or: [ { Type:"on mouse hover click" },{Type:"on mouse out"}, {Type : "on chat start"},{Type :"Load Event"} ] } },
       { $group: { _id:null , count: { $sum: 1 } } }
     ]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多