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
1 Answer
Reset to default 6You 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
版权声明:本文标题:javascript - Node.js to MongoDB: find by Date - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744201627a2594990.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论