Date query in mongo


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

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

How can specify last 20 second in query?


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

Thank you very match!

Or you can use moment subtract function (-: