Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
27
rated 0 times [  32] [ 5]  / answers: 1 / hits: 7263  / 10 Years ago, sun, march 30, 2014, 12:00:00

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



More From » node.js

 Answers
0

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.


[#46427] Saturday, March 29, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
keyanah

Total Points: 642
Total Questions: 93
Total Answers: 114

Location: Virgin Islands (U.S.)
Member since Tue, Jul 7, 2020
4 Years ago
;