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

后端 未结 7 2018
遇见更好的自我
遇见更好的自我 2020-12-03 04:41

I want to limit a query I\'m making to only look in documents that were created in the past 24 hrs.

What is the best way to structure this query? How do I go about

7条回答
  •  隐瞒了意图╮
    2020-12-03 05:07

    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.

提交回复
热议问题