admin管理员组

文章数量:1400652

Problem with find Date from Node to MongoDB:

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.

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);
        });
    });
};

The log files show empty result from MongoDB?:

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

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") }

// 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"
  },
...
]

Problem with find Date from Node to MongoDB:

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.

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);
        });
    });
};

The log files show empty result from MongoDB?:

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

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") }

// 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"
  },
...
]

Share Improve this question edited Mar 31, 2014 at 10:22 mylord asked Mar 30, 2014 at 18:11 mylordmylord 6992 gold badges12 silver badges20 bronze badges 1
  • It might solve your problem stackoverflow./a/12084462/2711647 – Durgesh Chaudhary Commented Mar 30, 2014 at 18:27
Add a ment  | 

1 Answer 1

Reset to default 6

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

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

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.

How to determine if the field is a string

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"),

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()

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.

本文标签: javascriptNodejs to MongoDB find by DateStack Overflow