Query to get last X minutes data with Mongodb

前端 未结 4 1640
有刺的猬
有刺的猬 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: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})
    

提交回复
热议问题