MongoDb时间戳

编程入门 行业动态 更新时间:2024-10-09 03:18:53
本文介绍了MongoDb时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经建立并想要汇入虚拟集合。每个项目中的字段之一是创建和更新字段。我可以放在source / json文件,以便MongoDb将使用当前的日期和时间作为导入的值?

这不工作

created:Date()

解决方案

mongoimport 用于以CSV,TSV或JSON格式导入数据 。如果要插入新字段(例如创建的时间戳),则必须为它们设置一个值。

例如,如果要将 created 时间戳设置为当前时间,则可以从命令行获取unix时间戳(这将是自纪元以来的秒数) :

$ date +%s 1349960286

JSON < 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 1349960286

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.

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时间戳

本文发布于:2023-11-22 00:32:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615286.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间   MongoDb

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!