Date query in mongo


#1

Hí,

I need a mongo query documents created at last 20 seconds.

Alarms.find({createdAt: {$gte: xxxxxx})

How can specify last 20 second in query?

Thx
Pal


#2

const lastSeconds = 20;

Converts to timestamp, subtracts 20 000 ms, converts timestamp back to date.
const dateLimit = new Date((new Date()).valueOf() - (lastSeconds * 1000));

Alarms.find({createdAt: {$gte: dateLimit })


#3

Thank you very match!


#4

Or you can use moment subtract function (-: