简体   繁体   English

用猫鼬和日期查询

[英]Querying with mongoose and dates

I'm trying to find out how to do an specific query with mongoose.我试图找出如何使用猫鼬进行特定查询。 I have in mongodb something like this:我在 mongodb 中有这样的东西:

{ "_id" : 1, "modificationDate" : ISODate("2013-06-26T18:57:30.012Z") }
{ "_id" : 2, "modificationDate" : ISODate("2013-06-26T18:57:35.012Z") }

I want to obtain all the objects where the difference between the actual date and modificationDate is greater than 5 days.我想获取实际日期和修改日期之间的差异大于 5 天的所有对象。

Calculate the 5-days-old cutoff time and then perform a find using the $lt operator and the calculated cutoff:计算 5 天前的截止时间,然后使用$lt运算符和计算出的截止时间执行find

var cutoff = new Date();
cutoff.setDate(cutoff.getDate()-5);
MyModel.find({modificationDate: {$lt: cutoff}}, function (err, docs) { ... });

If you need to query between two dates you can query with $gte = greater than equals and $lte = lesser than equals in date format "YYYY-MM-DD"如果您需要在两个日期之间进行查询,您可以使用 $gte = 大于等于和 $lte = 小于等于以日期格式“YYYY-MM-DD”进行查询

const results = await MyModel.find(
  {
    date: {
      $gte: "2021-01-20",
      $lte: "2021-02-15",
    }
  }
)

This example will query MyModel between January 20th 2021 to February 15th 2021.此示例将在 2021 年 1 月 20 日至 2021 年 2 月 15 日之间查询 MyModel。

Reference: https://mongoosejs.com/docs/tutorials/dates.html参考: https : //mongoosejs.com/docs/tutorials/dates.html

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

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