Query to get last X minutes data with Mongodb

前端 未结 4 1599
有刺的猬
有刺的猬 2020-12-04 16:11

I\'m beginning with mongodb, and I\'m trying to query my db that have this document format:

{ \"_id\" : ObjectId(\"520b8b3f8bd94741bf006033\"), \"value\" : 0         


        
相关标签:
4条回答
  • 2020-12-04 16:23

    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)

    0 讨论(0)
  • 2020-12-04 16:25

    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

    0 讨论(0)
  • 2020-12-04 16:33

    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})
    
    0 讨论(0)
  • 2020-12-04 16:42

    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}},
    ])
    
    0 讨论(0)
提交回复
热议问题