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

后端 未结 7 1999
遇见更好的自我
遇见更好的自我 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:21

    Use this in mongoose

    let ObjectId    = require('mongodb').ObjectID;
    
    Property.find({
        _id: {
            $gt: ObjectId.createFromTime(Date.now() / 1000 - 24 * 60 * 60)
        }
    }, (err, result) => {
        console.log(err || result);
    });
    

提交回复
热议问题