object

英[??bd??kt] US[?ɑ:bd?ekt]

n.Object; target; object; object, object

vi .Disapprove, oppose; feel disgusted

vt. Put forward... reasons for objection

id

英[?d]

n.ID card; genetic quality; instinctive impulse; identifier

MongoDB ObjectId function syntax

Function: ObjectId is a 12-byte BSON type data with the following format: the first 4 bytes represent the timestamp and the next 3 bytes are the two words immediately following the machine identification code The stanza consists of the process id (PID) and the last three bytes are random numbers. Documents stored in MongoDB must have an "_id" key. The value of this key can be of any type, and the default is an ObjectId object. In a collection, each document has a unique "_id" value to ensure that each document in the collection can be uniquely identified. The main reason why MongoDB uses ObjectId instead of other more conventional methods (such as automatically increasing primary keys) is because it is laborious and time-consuming to automatically increase primary key values ????on multiple servers simultaneously.

MongoDB ObjectId function example

創(chuàng)建新的ObjectId
使用以下代碼生成新的ObjectId:

>newObjectId = ObjectId()
上面的語句返回以下唯一生成的id:

ObjectId("5349b4ddd2781d08c09890f3")
你也可以使用生成的id來取代MongoDB自動生成的ObjectId:

>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
創(chuàng)建文檔的時(shí)間戳
由于 ObjectId 中存儲了 4 個(gè)字節(jié)的時(shí)間戳,所以你不需要為你的文檔保存時(shí)間戳字段,你可以通過 getTimestamp  函數(shù)來獲取文檔的創(chuàng)建時(shí)間:

>ObjectId("5349b4ddd2781d08c09890f4").getTimestamp()
以上代碼將返回 ISO 格式的文檔創(chuàng)建時(shí)間:

ISODate("2014-04-12T21:49:17Z")
ObjectId 轉(zhuǎn)換為字符串
在某些情況下,您可能需要將ObjectId轉(zhuǎn)換為字符串格式。你可以使用下面的代碼:

>new ObjectId().str
以上代碼將返回Guid格式的字符串::

5349b4ddd2781d08c09890f3