mongodb的id的唯一性_如何在mongodb中生成唯一的对象ID

从MongoDB Java Driver 3.3.0开始,有以下方法可以创建ObjectIds。

使用不带参数的构造函数:提供唯一的ObjectId

1. ObjectId id1 = new ObjectId(); //Generates unique id

1.1. ObjectId id2 = ObjectId.get(); //Calls new ObjectId();使用参数化构造函数:参数会影响ObjectId的唯一性

2. public ObjectId(byte[] bytes) // Receives a byte array of size 12.

3. public ObjectId(String hexString) //Receives a String that is a hexadecimal representation of 12 bytes.

4. public ObjectId(Date date) // Receives a Date object

5. public ObjectId(Date date, int counter) //Receives date and a counter

6. public ObjectId(Date date,

int machineIdentifier,

short processIdentifier,

int counter) //Receives Date, MachineId, PID and counter.

7. public ObjectId(int timestamp,

int machineIdentifier,

short processIdentifier,

int counter) //Receives Epoch time in sec, MachineId, PID and counter.了解ObjectId:

ObjectId由12个字节组成,分为以下几种:

ObjectID layout

0 1 2 3 4 5 6 7 8 9 10 11

|time |machine |pid |inc |

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值