【发布时间】:2014-08-16 07:51:01
【问题描述】:
我在 MongoDB 中有一个集合,其中包含 created 字段,其值当前存储为 BSON ISODate 对象。我想将所有这些转换为包含时间戳的 NumberLong 对象。
首先我尝试了这个(在 Mongo shell 中):
db.collection.find( { "created" : { "$type" : 9 } } ).forEach( function (x) {
x.created = new NumberLong(x.created);
db.collection.save(x);
});
JavaScript execution failed: Error: could not convert "Tue Mar 18 2014 18:11:21 GMT-0400 (EDT)" to NumberLong
显然,日期字符串不能被转换为长...足够公平。然后我尝试了这个,认为我可以利用 Javascript Date 对象的 UTC 方法:
db.collection.find( { "created" : { "$type" : 9 } } ).forEach( function (x) {
x.created = new Date(x.created).UTC();
db.collection.save(x);
});
JavaScript execution failed: TypeError: Object Tue Mar 18 2014 18:11:21 GMT-0400 (EDT) has no method 'UTC'
我尝试了其他几种变体,但都没有奏效。任何帮助将不胜感激!
【问题讨论】:
标签: mongodb date timestamp type-conversion bson