简体   繁体   English

如何使用 mongoose 在 mongodb 中的两天之间从 mongodb 获取数据

[英]How to get data from mongodb between including two days in mongodb using mongoose

Tried to get records from mongodb between including two days but in my code only i am getting between two days.试图在两天之间从 mongodb 获取记录,但在我的代码中只有两天之间。

Example:例子:

01-06-2020 to 08-06-2020 = getting records from 02-06-2020 to 08-06-2020(01-06-2020 missing) 01-06-2020 到 08-06-2020 = 获取 02-06-2020 到 08-06-2020 的记录(01-06-2020 缺失)

But i want 01-06-2020 to 08-06-2020 = need to get records from 01-06-2020 to 08-06-2020.但我想要 01-06-2020 到 08-06-2020 = 需要从 01-06-2020 到 08-06-2020 获取记录。

How to get it?如何得到它?

Mongodb Data: Mongodb 数据:

{ 
_id:ObjectId("5edd1df67b272e2d4cf36f70"),
pname:"Test 1", 
category:"Choco 1",
todaydate:2020-06-01T18:30:00.000+00:00
},
{ 
_id:ObjectId("5gdd1df67b272e2d4cf36f72"),
pname:"Test 2", 
category:"Choco 3",
todaydate: 2020-06-02T18:30:00.000+00:00
},
{ 
_id:ObjectId("5kdd1df67b272e2d4cf36f74"),
pname:"Test 5", 
category:"Choco 6",
todaydate: 2020-05-01T18:30:00.000+00:00
},
{ 
_id:ObjectId("5ewd1df67b272e2d4cf36f75"),
pname:"Test 6", 
category:"Choco 8",
todaydate: 2020-06-03T18:30:00.000+00:00
},
{ 
_id:ObjectId("5sdd1df67b272e2d4cf36f76"),
pname:"Test 3", 
category:"Choco 9",
todaydate: 2020-06-04T18:30:00.000+00:00
},
{ 
_id:ObjectId("5tdd1df67b272e2d4cf36f78"),
pname:"Test 11", 
category:"Choco 10",
todaydate: 2020-06-05T18:30:00.000+00:00
}

data.model.js:数据.model.js:

const mongoose = require('mongoose'); 
var userSchemaDate = new mongoose.Schema({ 
    pname: {
        type: String
    },  
    category: {
        type: String
    },  
    todaydate: {
        type: Date
    }   
}, {
    versionKey: false,
    collection: 'data'
}); 

module.exports = mongoose.model('Data', userSchemaDate);

data.controller.js:数据.controller.js:

module.exports.getReportTableData = (req, res, next) => {
    var collection = req.query.collection; 
    let tableReportdata = dbc.model(collection);

    let date1 = "01-06-2020"; dd/mm/yyyy
    let date2 = "07-06-2020"; dd/mm/yyyy

    tableReportdata.find({
            $and: [{
                    todaydate: {
                        $gt: date1
                    }
                },
                {
                    todaydate: {
                        $lt: date2
                    }
                }
            ]
        }, function(err, docs) {
            if (err) {
                console.log(err);
                return;
            } else {
                console.log("Successful loaded report data"); 
                res.json({ data: docs, msg: 'Report data loaded.' });
            }
        });
   }

The answer on your other question should return the correct result.您其他问题的答案应该返回正确的结果。 I'll also emphasise that it's better to store the date as date object.我还要强调,最好将日期存储为日期 object。

Let's try another approach by using $dateFromString on the input values as well.让我们尝试另一种方法,在输入值上也使用$dateFromString

tableReportdata.find({
  $expr: {
    $and: [
      {
        $gte: [
          {
            $dateFromString: {
              dateString: "$todaydate",
              format: "%d-%m-%Y",
              timezone: "UTC"
            }
          },
          {
            $dateFromString: {
              dateString: "01-06-2020",
              format: "%d-%m-%Y",
              timezone: "UTC"
            }
          }
        ]
      },
      {
        $lte: [
          {
            $dateFromString: {
              dateString: "$todaydate",
              format: "%d-%m-%Y",
              timezone: "UTC"
            }
          },
          {
            $dateFromString: {
              dateString: "07-06-2020",
              format: "%d-%m-%Y",
              timezone: "UTC"
            }
          }
        ]
      }
    ]
  }
}, function(err, docs) {
  if (err) {
    console.log(err);
    return;
  } else {
    console.log("Successful loaded report data"); 
    res.json({ data: docs, msg: 'Report data loaded.' });
  }
});

Shorter version with a helper function带助手的较短版本 function

const dateUTCexpr = (dateString) => ({
  $dateFromString: {
    dateString,
    format: "%d-%m-%Y",
    timezone: "UTC"
  }
})

tableReportdata.find({
  $expr: {
    $and: [
      {
        $gte: [dateUTCexpr("$todaydate"), dateUTCexpr("01-06-2020")]
      },
      {
        $lte: [dateUTCexpr("$todaydate"), dateUTCexpr("07-06-2020")]
      }
    ]
  }
}, function(err, docs) {
  if (err) {
    console.log(err);
    return;
  } else {
    console.log("Successful loaded report data"); 
    res.json({ data: docs, msg: 'Report data loaded.' });
  }
});

If you have todaydate defined as String in your schema, also make sure that it's properly converted in your database, you can use the following code如果您在架构todaydate定义为String ,还要确保它在您的数据库中正确转换,您可以使用以下代码

const dateUTCexpr = (dateString) => ({
  $dateFromString: {
    dateString,
    format: "%d-%m-%Y",
    timezone: "UTC"
  }
})

tableReportdata.find({
  todaydate: {
    $gte: dateUTCexpr("01-06-2020"),
    $lte: dateUTCexpr("07-06-2020")
  }
}, function(err, docs) {
  if (err) {
    console.log(err);
    return;
  } else {
    console.log("Successful loaded report data"); 
    res.json({ data: docs, msg: 'Report data loaded.' });
  }
});

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

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