Generating Mongo ObjectId (_id) with custom time?

你说的曾经没有我的故事 提交于 2019-12-01 00:15:29
mnemosyn

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.

EDIT: 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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!