日期范围内的Mongo Db搜索

时间:2018-07-16 15:16:41

标签: mongodb aggregation-framework

此查询返回0条记录,但db包含此范围内的许多记录

  db.player_sessions.aggregate([
  {
    "$project": {
      "date": {
        "$dateToString": {
          "format": "%Y-%m-%d",
          "date": {
            "$add": [
              "$CreatedOn",
              18000000
            ]
          }
        }
      },
      "dateWithOutHour": {
        "$dateToString": {
          "format": "%Y-%m-%d",
          "date": {
            "$add": [
              "$CreatedOn",
              18000000
            ]
          }
        }
      },
      "OsAndVersion": "$OsAndVersion",
      "BrandAndModel": "$BrandAndModel",
      "MobileOperator": "$MobileOperator",
      "CreatedOn": {
        "$add": [
          "$CreatedOn",
          18000000
        ]
      },

      "VendorID": "$channel.VendorID"
    }
  },
  {
    "$match": {
      "Type": "Channel",
      "PurchaseType": "Subscription",
 "CreatedOn": {
        "$gte": {
          "$date": {
            "$numberLong": "1516114950000"
          }
        },
    "$lte": {
      "$date": {
        "$numberLong": "1531094399000"
      }
    }
  }

    }
  },
  {
    "$group": {
      "_id": null,
      "Views": {
        "$sum": 1
      },
      "WatchTime": {
        "$sum": "$PlayedDuration"
      },
      "AverageWatchTime": {
        "$avg": "$PlayedDuration"
      },
      "UniqueUsers": {
        "$addToSet": "$UserID"
      }
    }
  },
  {
    "$project": {
      "_id": 0,
      "WatchTime": "$WatchTime",
      "Views": 1,
      "AverageWatchTime": "$AverageWatchTime",
      "UniqueUsers": {
        "$size": "$UniqueUsers"
      }
    }
  }
])

如果删除了$ match中的日期范围,则查询返回数据,请验证汇总部分中创建的日期范围实现是否正确,并且此查询是从特定日期范围中选择记录并将其按类型和计算进行分组

0 个答案:

没有答案