我一直在使用node-mongoskin连接这两个.一切都很好,直到我查询了一些"日期"字段,我认为该字段应该作为javascript的Date对象返回.但结果的类型是字符串,这是奇怪的(对我而言)并且不方便.
插入看起来像这样:
var doc = {
date: new Date(),
info: 'Some info'
}
db.users.insert( doc, {safe: true}, function(err, res) {
...
});
Run Code Online (Sandbox Code Playgroud)
以上结果是(没有_id字段):
{ "date" : "Mon Oct 24 2011 18:00:57 GMT+0400 (MSK)", "info": "Some info" }
Run Code Online (Sandbox Code Playgroud)
但是,使用MongoDB Shell插入工作正常,除了字段类型 ISODate
> db.things.insert({ date: new Date() }); db.things.find();
{ "_id" : ObjectId("4eae9f2a34067b92db8deb40"), "date" : ISODate("2011-10-31T13:14:18.947Z") }
Run Code Online (Sandbox Code Playgroud)
所以,问题是:我应该如何插入文档来查询日期字段作为Date对象?我想要的是在数据库服务器端设置字段.我只是发送类似null-fields的东西,db-server使用默认的mongo机制为我设置这些.
插入时间戳(作为本机MongoDB时间戳)也是一个问题,但它并不是什么大问题.
PS:没有运气通过mongoskin和mongodb-native docs.
Ale*_*kii 13
这可能是我的代码或mongo驱动程序中的一些错误.现在,以下工作正常:
db.collection.insert({d: new Date()});
Run Code Online (Sandbox Code Playgroud)
此处描述的时间戳支持:http://mongodb.github.com/node-mongodb-native/api-bson-generated/timestamp.html.
| 归档时间: |
|
| 查看次数: |
16293 次 |
| 最近记录: |