Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Understand the default ObjectID of MongoDB

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

BSON ObjectID Specification

A BSON ObjectID is a 12-byte value consisting of a 4-byte timestamp (seconds since epoch), a 3-byte machine id, a 2-byte process id, and a 3-byte counter. Note that the timestamp and counter fields must be stored big endian unlike the rest of BSON. This is because they are compared byte-by-byte and we want to ensure a mostly increasing order. The format:

01234567891011timemachinepidincTimeStamp. This is a unix style timestamp. It is a signed int representing the number of seconds before or after January 1st 1970 (UTC). Machine. This is the first three bytes of the (md5) hash of the machine host name, or of the mac/network address, or the virtual machine id.Pid. This is 2 bytes of the process id (or thread id) of the process generating the object id.Increment. This is an ever incrementing value, or a random number if a counter can't be used in the language/runtime.

BSON ObjectIds can be any 12 byte binary string that is unique; however, the server itself and almost all drivers use the format above.

The instructions and results for viewing ObjectId in segments are as follows:

> db.test.findOne (). _ id.toString () ObjectId ("50c6b336ba95d7738d1042e3") > db.test.findOne (). _ id.toString (). Substring (10Lei18) 50c6b336 > db.test.findOne (). _ id.toString (). Substring (18pr 24) ba95d7 > db.test.findOne (). _ id.toString (). Substring (24d28) 738d > db.test.findOne (). _ id.toString (). Substring (28pai34) 1042e3

ObjectId occupies 12 bytes of storage space and consists of "timestamp", "machine name", "PID number" and "counter". The advantage of using machine names is that the performance bottleneck of single point counting can be avoided in a distributed environment. The advantage of using PID number is that it supports multiple mongod instances running on the same machine. Finally, the combination of timestamp and counter is used to ensure uniqueness.

Time stamp

Ensuring the uniqueness of ObjectId depends on the order of time, not on the value of time, so the time of cluster nodes does not have to be completely synchronized. Now that ObjectId has a timestamp, you can save a timestamp in the document. When using ObjectID to extract time, you should be aware of the detail that MongoDB allows time inconsistencies across nodes.

Here are two ways to view the timestamp:

> db.test1.findOne (). _ id.getTimestamp () ISODate ("2012-12-12T03:52:45Z") > Date (parseInt (db.test1.findOne (). _ id.toString (). Substring (10Ling 18), 16)) Wed Dec 12 2012 12:11:02 GMT+0800

Machine name

The first three bytes of the machine name are encrypted through Md5, so there should be a probability of repetition. It is always possible to check when configuring the production cluster. In addition, I also noticed that the result of MD5 encryption will change after restarting MongoDB, so you need to be careful when using ObjectID to extract machine name information.

PID number

Notice that the PID number usually changes each time the mongod process is restarted.

Counter

The counter occupies 3 bytes, which means that the range of values is 256 "256" 256-1 "16777215. Think of the limit of MongDB performance as 10 million records per second on a single device. At the current level, it would be nice for a single device to insert 10, 000 messages a second, so the design of the ObjectID counter is sufficient.

Some records are inserted in the loop, and b is the loop counter in the following query, so you can see that the ObjectId counters on my machine are increased in order:

> parseInt (db.test.findOne ({bju 1000}). Substring (28 id.toString 34), 16) 1947382 > parseInt (db.test.findOne ({bju 1001}). Substring (28 id.toString), 16) 1947383 > parseInt (db.test.findOne ({bju 1002}). _ id.toString (). Substring (28 Magnum 34), 16) 1947384 > parseInt (db.test.findOne ({bju 1003}). _ id.toString (). Substring (28 Magnum 34), 16) 1947385

The following code is derived from: http://www.cnblogs.com/xjk15082/archive/2011/09/18/2180792.html

Build objectIdpublic class ObjectId implements Comparable, java.io.Serializable {final int _ time;final int _ machine;final int _ inc;boolean _ new;public ObjectId () {_ time = (int) (System.currentTimeMillis () / 1000); _ machine = _ genmachine;_inc = _ nextInc.getAndIncrement (); _ new = true;}. } Generation of machine code and process code private static final int _ genmachine;static {try {final int machinePiece; {StringBuilder sb = new StringBuilder (); Enumeration e = NetworkInterface.getNetworkInterfaces (); while (e.hasMoreElements ()) {NetworkInterface ni = e.nextElement (); sb.append (ni.toString ());} machinePiece = sb.toString (). HashCode ()

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report