MongoDb时间戳 [英] MongoDb timestamp
问题描述
我已经创建并想要导入一个虚拟集合。每个项目中的一个字段是创建和更新字段。我可以在source / json文件中添加什么内容,以便MongoDb将使用当前的日期和时间作为导入的值?
i have created and want to now import a dummy collection. one of the fields in each item are "created" and "updated" fields. what can i put in the source/json file so that MongoDb will use the current date and time as the value on import?
这个不工作
"created" : Date()
推荐答案
mongoimport
用于以CSV,TSV或JSON格式导入现有的数据。如果要插入新字段(例如创建的
timestamp),则必须为它们设置一个值。
mongoimport
is intended for importing data existing data in CSV, TSV, or JSON format. If you want to insert new fields (such as a created
timestamp) you will have to set a value for them.
例如,如果要将创建的
时间戳设置为当前时间,可以从命令行获取一个unix时间戳(从时间开始是秒) :
For example, if you want to set the created
timestamp to the current time, you could get a unix timestamp from the command line (which will be seconds since the epoch):
$ date +%s
1349960286
$ < date>
表示 mongoimport
expects是一个64位有符号整数,表示毫秒,时代。您需要将unixtime秒值乘以1000并将其包含在您的JSON文件中:
The JSON <date>
representation that mongoimport
expects is a 64-bit signed integer representing milliseconds since the epoch. You'll need to multiply the unixtime seconds value by 1000 and include in your JSON file:
{ "created": Date(1349960286000) }
另一种方法是将已创建的时间戳添加到文档插入后。
An alternative approach would be to add the created timestamps to documents after they have been inserted.
例如:
db.mycoll.update(
{created: { $exists : false }}, // Query criteria
{ $set : { created: new Date() }}, // Add 'created' timestamp
false, // upsert
true // update all matching documents
)
这篇关于MongoDb时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!