Goku
Goku

Reputation: 1840

Query to get last X minutes data with Mongodb

I'm trying to query my db that have this document format:

{
  "_id" : ObjectId("520b8b3f8bd94741bf006033"),
  "value" : 0.664,
  "timestamp" : ISODate("2013-08-14T13:48:35Z"),
  "cr" : ISODate("2013-08-14T13:50:55.834Z")
}

I can get the last records from a datetime with this query:

db.mycol.find({timestamp:{$gt: ISODate("2013-08-14T13:48:00Z")}}).sort({x:1});

But I'm trying to get a set with the value fields and timestamps from 18 minutes ago.

Upvotes: 33

Views: 56334

Answers (6)

Cherif KAOUA
Cherif KAOUA

Reputation: 844

const xMins = 10;

db.mycol.find({ timestamp: { $gt: new Date(Date.now() - 1000 * 60 * xMins) } }).sort({ x: 1 });

Upvotes: 0

Xavier Guihot
Xavier Guihot

Reputation: 61676

Starting in Mongo 5, you can use $dateSubtract:

// { date: ISODate("2021-12-05T20:32:56Z") } <= 5  minutes ago
// { date: ISODate("2021-12-05T20:07:56Z") } <= 25 minutes ago (older than 18 minutes)
db.collection.aggregate([
  { $match: {
    $expr: {
      $gt: [
        "$date",
        { $dateSubtract: { startDate: "$$NOW", unit: "minute", amount: 18 } }
      ]
    }
  }}
])
// { date: ISODate("2021-12-05T20:32:56Z") } <= 5 minutes ago

With $dateSubtract, we create the oldest date/time after which we keep documents, by subtracting 18 (amount) "minute" (unit) out of the current date $$NOW (startDate).

Upvotes: 5

Pravin Bansal
Pravin Bansal

Reputation: 4681

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)

Upvotes: 13

Mukesh Kashyap
Mukesh Kashyap

Reputation: 909

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

Upvotes: 2

Goku
Goku

Reputation: 1840

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})

Upvotes: 1

H.D.
H.D.

Reputation: 4454

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}},
])

Upvotes: 76

Related Questions