我一直在使用node-mongoskin连接这两个。一切正常,直到我查询了一些“日期”字段,我认为该字段应作为javascript的Date
对象返回。但是result的类型是字符串,这对我来说很奇怪并且不方便。
插入看起来像这样:
var doc = {
date: new Date(),
info: 'Some info'
}
db.users.insert( doc, {safe: true}, function(err, res) {
...
});
上面的结果是(没有
_id
字段):{ "date" : "Mon Oct 24 2011 18:00:57 GMT+0400 (MSK)", "info": "Some info" }
但是,使用MongoDB Shell插入即可正常工作,但字段类型为
ISODate
> db.things.insert({ date: new Date() }); db.things.find();
{ "_id" : ObjectId("4eae9f2a34067b92db8deb40"), "date" : ISODate("2011-10-31T13:14:18.947Z") }
因此,问题是:我应该如何将文档作为
Date
对象插入查询日期字段?我想要在数据库服务器端设置字段。我只是发送诸如null字段之类的内容,而db-server使用默认的mongo机制为我设置这些内容。插入时间戳(如native MongoDB timestamp)也是一个问题,但这并不是什么大问题。
PS:碰巧遇到过mongoskin和mongodb-native文档。
最佳答案
这可能是我的代码或mongo驱动程序中的某些错误。现在,以下工作正常:
db.collection.insert({d: new Date()});
时间戳支持在此处描述:http://mongodb.github.com/node-mongodb-native/api-bson-generated/timestamp.html。
关于MongoDB + Node.js : unable to insert date properly,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7954381/