如何在mongodb中生成唯一对象id [英] How to generate unique object id in mongodb

查看:155
本文介绍了如何在mongodb中生成唯一对象id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用Mongodb和Java时,我想在客户端生成Object id。但是,在插入记录之前,我必须首先查询mongodb以确保ObjectId()方法生成的id是唯一的。有没有什么方法可以生成唯一的对象ID而无需两次访问mongodb?

When I use Mongodb with Java, I want to generate Object id at clients. Before I insert a record, however, I have to query mongodb first to make sure that the id generated by ObjectId() method is unique. Is there any way that I can generate unique object id without accessing mongodb twice?

推荐答案

对象ID与您使用的顺序ID不同在RDMS中。如果根据对象ID规范正确生成它们,则无需担心它们是唯一的。

Object IDs are not like sequential ids you use in a RDMS. If they are properly generated according to the Object ID specification you will not need to worry about them being unique.

你所要做的就是确保你总是创建一个新的对象ID,而不是重复使用它们。

All you have to do is ensure you always create a new Object ID rather than reusing them.

这篇关于如何在mongodb中生成唯一对象id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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