Mongo 从现有日期字段添加时间戳字段 [英] Mongo add timestamp field from existing date field
问题描述
我目前有一个包含以下文档的集合:
I currently have a collection with documents like the following:
{ foo: 'bar', timeCreated: ISODate("2012-06-28T06:51:48.374Z") }
我现在想为这个集合中的文档添加一个 timestampCreated
键,以便更容易地按时间查询.
I would now like to add a timestampCreated
key to the documents in this collection, to make querying by time easier.
我能够使用 update
和 $set
操作添加新列,并设置时间戳值,但我似乎正在使用此设置当前时间戳:
I was able to add the new column with an update
and $set
operation, and set the timestamp value but I appears to be setting the current timestamp using this:
db.reports.update({}, {
$set : {
timestampCreated : new Timestamp(new Date('$.timeCreated'), 0)
}
}, false, true);
但是,我无法找到添加此列并将其值设置为现有timeCreated"字段的时间戳的方法.
I however have not been able to figure out a way to add this column and set it's value to the timestamp of the existing 'timeCreated' field.
推荐答案
查找所有文档,仅限于 id 和 timeCreated 字段.然后循环并生成 timestampCreated 值,并对每个值进行更新.
Do a find for all the documents, limiting to just the id and timeCreated fields. Then loop over that and generate the timestampCreated value, and do an update on each.
这篇关于Mongo 从现有日期字段添加时间戳字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!