我已经建立并想要汇入虚拟集合。每个项目中的字段之一是创建和更新字段。我可以放在source / json文件,以便MongoDb将使用当前的日期和时间作为导入的值?
这不工作
created:Date()解决方案
mongoimport 用于以CSV,TSV或JSON格式导入数据 。如果要插入新字段(例如创建的时间戳),则必须为它们设置一个值。
例如,如果要将 created 时间戳设置为当前时间,则可以从命令行获取unix时间戳(这将是自纪元以来的秒数) :
$ date +%s 1349960286JSON < date> 表示 mongoimport 期望是一个64位有符号整数,表示自时期以来的毫秒。您需要将unixtime秒值乘以1000并包含在JSON文件中:
{created:Date另一种方法是在文档插入后将创建的时间戳添加到文档中。另一种方法是在文档插入后将创建的时间戳添加到文档中。例如:
db.mycoll.update $ b {created:{$ exists:false}},//查询条件 {$ set:{created:new Date()}},//添加'created'timestamp false,// upsert true //更新所有匹配的文档)
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?
this wont work
"created" : Date()解决方案
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.
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 1349960286The 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.
For example:
db.mycoll.update( {created: { $exists : false }}, // Query criteria { $set : { created: new Date() }}, // Add 'created' timestamp false, // upsert true // update all matching documents )
更多推荐
MongoDb时间戳
发布评论