I have a dataset similar to the following:
{
"_id" : ObjectId("5a4c6fb6993a721b3479a27e"),
"score" : 8.3,
"page" : "@message",
"lastmodified" : ISODate("2018-01-03T06:49:19.232Z"),
"createdate" : ISODate("2018-01-03T05:52:54.446Z"),
"slug" : [
"@APPLE"
],
"__v" : 0
},
{
"_id" : ObjectId("5a4c6fb6993a721b3479a27e"),
"score" : 9.3,
"page" : "@BANANA",
"lastmodified" : ISODate("2018-01-03T06:49:19.232Z"),
"createdate" : ISODate("2018-01-03T05:52:54.446Z"),
"slug" : [
"@APPLE"
],
"__v" : 0
}
{
"_id" : ObjectId("5a4c6fb6993a721b3479a27e"),
"score" : 5.3,
"page" : "@BANANA",
"lastmodified" : ISODate("2018-01-03T06:49:19.232Z"),
"createdate" : ISODate("2018-01-03T05:52:54.446Z"),
"slug" : [
"@BANANA"
],
"__v" : 0
}
My objective is to compute the total sum of scores based on specified filters, like so:
@APPLE: 8.3+9.3 = 17.6 for @APPLE : 17.6,
@BANANA: 9.3+5.3 = 14.6 for @BANANA: 14.6
To achieve this, I need to extract data from the past hour instead of scanning through the entire database. The query for this purpose looks like:
var newTime = new Date();
newTime.setHours( newTime.getHours() - 1 );
db.Test.find({"lastmodified":{$gt: newTime}})
Thus, with this query, I can access only the data from the last hour. However, I am facing confusion regarding how to calculate the sum with these specific filters applied. My attempt at filtering using the following query has not returned any results:
db.Test.find({"lastmodified": {$gt: newTime}}, {$or: [{slug: {$in: ['@APPLE']}}, {page: '@APPLE'}]})
If anyone could offer assistance on this issue, it would be greatly appreciated.