简体   繁体   English

如何从仅在最后几分钟内插入的 MongoDB 获取文档?

[英]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.但是对于我的用例,我希望在几分钟前插入文档,比如 5 分钟。 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 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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