MongoDB: Only fetch documents created in the last 24hrs?

Add createdAt field, index it, then query

db.getCollection("COLLECTION_NAME").find({"createdAt":{$gt:new Date(Date.now() - 24*60*60 * 1000)}})

This will return all records older then 86400 seconds.

Leave a Comment