简体   繁体   English

如何使用 mongoose 从 mongodb 获取两个日期之间的数据

[英]How to get data between the two dates from mongodb using mongoose

My question is simple.Tried to get record from mongodb between the two dates like[ 01-06-2020 to 07-06-2020].But not working.How to do it?我的问题很简单。试图在两个日期之间从 mongodb 获取记录,例如 [01-06-2020 到 07-06-2020]。但不工作。怎么做?

Date format is dd-mm-yyyy日期格式为 dd-mm-yyyy

mongodb records: mongodb 记录:

[
{ 
_id:ObjectId("5edd1df67b272e2d4cf36f70"),
 date:"01-06-2020", 
 pid:1,
 pname:"Micheck" 
},
{ 
_id:ObjectId("5edd1dk67b272e2d4cf31f72"),
 date:"03-06-2020", 
 pid:2,
 pname:"Zohn" 
},
{ 
_id:ObjectId("5edd1rf67b272e2d4cf16f73"),
 date:"07-06-2020", 
 pid:3,
 pname:"Robert" 
},
{ 
_id:ObjectId("5edd1dw67b272e2d4cf76f76"),
 date:"01-05-2020", 
 pid:6,
 pname:"Josebh" 
}
]

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

module.exports.getReportTableData = (req, res, next) => {
    let collectionname = req.query.collection; 

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

    let tableReportdata = dbc.model(collectionname);
    tableReportdata.find({ date: date1,date2 }, function(err, docs) {
        if (err) {
            console.log(err);
            return;
        } else {
            console.log("Successful loaded data");
            res.json({ data: docs, success: true, msg: 'Data loaded.' });
        }
    });
};

This solution Needs Mongo server version > 4.0.0此解决方案需要 Mongo 服务器版本 > 4.0.0

According to OPs comment he's using version 4.2.7根据 OP 的评论,他使用的是 4.2.7 版

Mongo is currently treating your fields as a string, and not a date, so first you'll have to convert them to dates. Mongo 当前将您的字段视为字符串,而不是日期,因此首先您必须将它们转换为日期。

I would recommend changing your field type to date while writing to the DB, or adding a new field with date type for better performance and less overhead我建议在写入数据库时将字段类型更改为日期,或者添加具有日期类型的新字段以获得更好的性能和更少的开销

To do this conversion at runtime, you'll have to use the aggregation pipeline with the, addFields dateFromString match gte and lte operators.要在运行时进行此转换,您必须将聚合管道与addFields dateFromString 匹配gtelte运算符一起使用。

Your code should look like:您的代码应如下所示:

let date1 = new Date("2020-06-01T00:00:00.000Z"); // date objects
let date2 = new Date("2020-06-07T00:00:00.000Z");

let tableReportdata = dbc.model(collectionname);
tableReportdata.aggregate([{
        $addFields: {
            convertedDate: {
                $dateFromString: {
                    dateString: "$date",
                    format: "%d-%m-%Y",
                    timezone: "UTC"
                }
            }
        }
    },
    {
        $match: {
            convertedDate: {
                $gte: date1,
                $lte: date2,

            }
        }
    }
], function(err, docs) {
    if (err) {
        console.log(err);
        return;
    } else {
        console.log("Successful loaded data");
        res.json({
            data: docs,
            success: true,
            msg: 'Data loaded.'
        });
    }
});

Playground Link游乐场链接

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

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