NodeJS MongoDB:查询ISO日期

时间:2018-02-10 04:45:24

标签: node.js mongodb

我将MongoDB中存储的ISO日期集合作为字符串,如下所示:

{ "date": "2014-12-12T03:33:33.333Z" },
{ "date": "2014-12-13T03:33:33.333Z" }

在控制台中,我可以使用

完美地查询这些内容
{ "date": ISODate("2014-12-12T03:44:00.000Z") }

但是,我使用NodeJS驱动程序,我无法使用ISODate。我在这里找到了几个与此问题相关的问题,但是所提出的解决方案似乎都没有效果。例如:

// These does not find any matches
db.find({ "date": new Date("2014-12-12T03:44:00.000Z") })
db.find({ "date": { '$eq': '2014-12-12T03:44:00.000Z' } })
db.find({ "date": { '$eq': new Date('2014-12-12T03:44:00.000Z') } })

//This throws an error stating $date is not an operator
db.find({ "date": { '$date': '2014-12-12T03:44:00.000Z' } })

为什么这些查询失败?

编辑:这是另一个样本,直接来自数据库:

{
    "_id": "5a7e88f34b5916723589183f",
    "date": "2014-12-12T03:42:00.000Z",
    "granularity": 180
}

编辑2:此查询产生以下错误MongoError: $dateFromString requires that 'dateString' be a string, found: date with value 2014-12-12T03:44:00.000Z

async loadCandle(date, granularity) {        
        date = date + ''; //Aded to ensure date is a string, but still get the error.
        var candle = await this.data.collection('dates').findOne( 
            { $expr : 
                {$eq : 
                    [
                        {$dateFromString : {dateString : "$date"}}, 
                        new Date("2014-12-12T03:33:33.333Z") //Normally would pass in the `date` variable here
                    ]
                } });

2 个答案:

答案 0 :(得分:0)

因为$date不是运算符

您需要使用$dateFromString将字符串日期转换为ISODate进行比较

db.datez.find(
    {$expr : 
        {$eq : 
            [
                {$dateFromString : {dateString : "$date"}}, 
                new Date("2014-12-12T03:33:33.333Z")
            ]
        }
    }
)

使用聚合

db.datez.aggregate([
    {$match : 
        {$expr : 
            {$eq : 
                [
                    {$dateFromString : {dateString : "$date"}}, 
                    new Date("2014-12-12T03:33:33.333Z")
                ]
            }
        }
    }
])

集合

> db.datez.find()
{ "_id" : ObjectId("5a7e795e80aae386f73cf0fe"), "date" : "2014-12-12T03:33:33.333Z" }
{ "_id" : ObjectId("5a7e795e80aae386f73cf0ff"), "date" : "2014-12-13T03:33:33.333Z" }
> 

结果

> db.datez.find({$expr : {$eq : [{$dateFromString : {dateString : "$date"}}, new Date("2014-12-12T03:33:33.333Z")]}})
{ "_id" : ObjectId("5a7e795e80aae386f73cf0fe"), "date" : "2014-12-12T03:33:33.333Z" }

答案 1 :(得分:0)

您可以使用$ dateToString运算符生成任何指定格式的字符串日期,以便以后进行比较。

对于日期的字符串比较,输入格式应为YYYY-MM-DD,其他任何格式的日期查询都将失败

让我通过示例进行说明:

这是我在mongoDb中的收藏集:

{
    "_id" : ObjectId("5f2d0a0c632ec022e08c3191"),
    "date" : ISODate("2020-07-12T00:00:00Z")
}
{
    "_id" : ObjectId("5f2d0a12632ec022e08c3192"),
    "date" : ISODate("2020-07-13T00:00:00Z")
}

现在要从Node触发以比较存储的ISODates的查询如下

db.collection.aggregate(
    [
        {
            $addFields: {
                formattedDate: { // An extra field "formattedDate" is added in each document which can be compared later through pipeline using $match
                    $dateToString: {
                        format: "%Y-%m-%d",
                        date: "$date" // in "$date" date is variable from db
                    }
                }
            }
        },
        {
            $match: {
                formattedDate: {
                    $eq: "2020-07-12" // here you can provide your input date yyyy-mm-dd
                }
            }
        }
    ]
)

因此对于以上查询,您将获得输出为

{
    "_id" : ObjectId("5f2d0a0c632ec022e08c3191"),
    "date" : ISODate("2020-07-12T00:00:00Z"),
    "formattedDate" : "2020-07-12"
}

希望这对您或其他人有帮助!