简体   繁体   中英

How to fetch a document from MongoDB inserted only within last few minutes?

I am using the below query to fetch the latest document.

const user = await Conversation.find({ isBot: false, to: from.slice(9), queryType: "template_preview" }).sort({ sentTime: -1 }).limit(1);

But the issue is this can also fetch old documents inserted long before. But for my use case I want the document inserted before few minutes say 5 min. What should be the exact query for this.

Try this one:

const user = await Conversation.find({ 
         isBot:false,
         sendTime:{
            $gte: timestamp of -5 min,
            $lte: timestamp (now)
         },
         queryType: "template_preview"
     }).sort({ sentTime: -1 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM