生成具有自定义时间的Mongo ObjectId(_id)? [英] Generating Mongo ObjectId (_id) with custom time?

查看:280
本文介绍了生成具有自定义时间的Mongo ObjectId(_id)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一张文章表从MySQL移植到Mongo DB.我知道Mongo生成的_id字段具有创建时间,可以将其提取或查询它.因此,我想使用它从MySQL获取我的created_time INT时间戳.移动数据以为Mongo生成_id时,是否有一种方法可以将当前具有的时间戳记记录在单独的字段中?

I am porting over a table of articles from MySQL to Mongo DB. I understand that the _id field generated by Mongo has the time of creation somehow in there and can be extracted or you can query against it. Because of this, I want to use it to have my created_time INT timestamp from MySQL. Is there a way when moving my data to generate an _id for Mongo that will have the time stamp that I currently have for my records in a separate field?

推荐答案

是的,这是可能的.例如,在C#驱动程序中,有一个ObjectId的构造函数,您可以在其中传递时间戳,请参见此源文件.使用帮助程序方法GenerateNewId:

Yes, that's possible. In the C# driver for instance, there's a constructor for ObjectId where you can pass in a timestamp, see this source file. It's easiest to use the helper method GenerateNewId:

var id = ObjectId.GenerateNewId(new DateTime(2012, 01, 01));

我不知道您使用的是哪种编程语言,也不知道该语言的驱动程序是否支持此功能,但是我想这是广泛可用的.

I don't know which programming language you're using and whether the driver in that language supports this feature, but I guess this is widely available.

此问题包含Java,猫鼬和python的代码,答案包含PHP的代码. 广泛可用"似乎是一个错误的假设,例如,尚未在PHP驱动程序中实现.

This question contains the code for Java, mongoose and python, and the answer contains code for PHP. "Widely available" seems to be a wrong assumption, it's not yet implemented in the PHP driver, for instance.

这篇关于生成具有自定义时间的Mongo ObjectId(_id)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆