Query to get last X minutes data with Mongodb
you can access the data of current timestamp from mongodb using nodejs
const collection1 = dbo.collection('customers');
var dateq = new Date();
collection1.find({ "Timestamp" : { $gt: new Date(dateq.getTime() - 6000)}
}).toArray(function(err , docs){
console.log(docs);
}
code end
Wow, thanks to @Alistair_Nelson I was able to get the data from n minutes ago, for example to get the last 18 minutes from ISODate("2013-08-14T14:00:00Z"):
db.mycol.find({timestamp:{$gt: new Date(ISODate("2013-08-14T14:00:00Z")-18*60000)}})
To get only the fields I need:
db.mycol.find({timestamp:{$gt: new Date(ISODate("2013-08-14T14:00:00Z")-18*60000)}},{value:1,timestamp:1, _id:0})
For the 18 minutes part, that's not really about MongoDB, but about JavaScript and what's available in the mongo shell:
query = {
timestamp: { // 18 minutes ago (from now)
$gt: new Date(ISODate().getTime() - 1000 * 60 * 18)
}
}
Works in the mongo shell, but using Mongo drivers for other languages would be really different.
To "project" over a smaller schema with both values and timestamps:
projection = {
_id: 0,
value: 1,
timestamp: 1,
}
Applying both:
db.mycol.find(query, projection).sort({timestamp: 1});
Well, that's still not a "set" since there might be duplicates. To get rid of them you can use the $group
from the aggregation framework:
db.mycol.aggregate([
{$match: query},
{$group: {
_id: {
value: "$value",
timestamp: "$timestamp",
}
}},
{$project: {
value: "$_id.value",
timestamp: "$_id.timestamp",
}},
{$sort: {timestamp: 1}},
])
You could also do below
db.getCollection('collectionName').find({timestamp : {$gte: new Date().getTime()-(60*60*1000) } } )
The above query ll give you records of timestamp b/w now and 60 mins. if you like more then 60 mins - say 2 hrs you could change expression to (2*60*60*1000) for 30 mins (30*60*1000)