Node.js到MongoDB:按日期查找 [英] Node.js to MongoDB: find by Date

查看:189
本文介绍了Node.js到MongoDB:按日期查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从节点到MongoDB的查找日期问题:

Problem with find Date from Node to MongoDB:

据说MongoDB可能没有存储Date对象,而是一个字符串,但我不知道如何检查或如何修复。

It's been said MongoDB might not be storing a Date object, but a string, but I'm not sure how to check, or how to fix that.

在我的turnys.js文件中:

In my turnys.js file:

exports.findNeededTurnys = function(req, handler)
{
    console.log("findNeededTurnys");
    var key;
    //var arg0 = {$or:[{start:{$lte:new Date()}, $where: "this.users.length == this.seats"}]}; 
    var arg0 = {start:{$lte:new Date()}};
    console.log("findNeededTurnys: arg0="+JSON.stringify(arg0));
    turnydb.collection('turnys', function(err, collection)
    {
        collection.find(arg0, {safe:true}, function(err, result) {
            if(err) console.log("findNeededTurnys: find: err="+err);
            console.log("findNeededTurnys: find: result="+JSON.stringify(result));
            handler.handle(result);
        });
    });
};

日志文件显示MongoDB的空白结果?

The log files show empty result from MongoDB?:

findNeededTurnys: arg0={"start":{"$lte":"2014-03-31T10:17:48.857Z"}}
findNeededTurnys: find: result={}

在Mongo中,查询工作(添加新的Date调用者之后),因为驱动程序可能会在js console.log中抽象出来):

In Mongo, the query works (after adding new Date caller, as driver might abstract that in the js console.log):

> db.turnys.find({"start":{"$lte":"2014-03-31T10:17:48.857Z"}});
> db.turnys.find({"start":{"$lte":new Date("2014-03-31T10:17:48.857Z")}});
{ "gId" : ObjectId("5335e4a7b8cf51bcd054b423"), "seats" : 2, "start" : ISODate("2014-03-31T08:47:48.946Z"), "end" : ISODate("2014-03-31T08:49:48.946Z"), "rMin" : 800, "rMax" : 900, "users" : [ ], "_id" : ObjectId("53392bb42b70450000a834d8") }

//这里是一个mongo db的示例

// here is a sample of the mongo db

[
  {
    "gId": "5335e4a7b8cf51bcd054b423",
    "seats": 2,
    "start": "2014-03-31T08:47:48.946Z",
    "end": "2014-03-31T08:49:48.946Z",
    "rMin": 800,
    "rMax": 900,
    "users": [],
    "_id": "53392bb42b70450000a834d8"
  },
  {
    "gId": "5335e4a7b8cf51bcd054b423",
    "seats": 2,
    "start": "2014-03-31T08:47:48.946Z",
    "end": "2014-03-31T08:49:48.946Z",
    "rMin": 1000,
    "rMax": 1100,
    "users": [],
    "_id": "53392bb42b70450000a834da"
  },
...
]

推荐答案

你不需要任何这种包装。日期是日期

You do not need any of this wrapping. A date is a date:

var zeroth = {$or:[ {start: new Date(), {users:{$size:2}} ]}; 

当然,如果文档中的这些日期实际上是字符串而不是正确的 date 类型,那就是你的问题。而真正需要修正这些值,以便它们是真实的日期。

Now of course if those "dates" in your document are actually "strings" and not proper date types then that is your problem. And what you really need to to is fix those values so they are real dates.

这适用于任何语言实现,您应该使用本机日期类型,并让驱动程序为您进行转换。

This applies to any language implementation, where you should be working with the native "date" type and let the driver do the conversion for you.

很明显的区别是,当您查看mongo shell中的文档时,如果它是一个真正的BSON日期类型,那么它将如下所示:

Well the clear difference is when you look at a document in the mongo shell, if it is a real BSON date type then it will look like this:

"start": ISODate("2014-03-31T08:47:48.946Z"),

如果这还不够清楚,那么有一个 $ type 这样查询:

If that is not clear enough then there is the $type operator which you can use in a query like this:

db.collection.find({ "start": { "$type": 2 } }).count()

它也是 MongoDB,如果它是一个字符串,但在应用程序或导入中负责创建此错误实施。在初始回应中所提出的要点是关于这一点。

It is also not MongoDB that is doing this if it is a string, but rather bad implementation in the application or import that was responsible from creating this. Which was what the points made in the initial response were all about.

这篇关于Node.js到MongoDB:按日期查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆