user3710799
user3710799

Reputation: 3

new MongoId() - Is it a write operation or read operation

I am using mongodb with php. I want to know when we create a new mongo id using new MongoId() How does it consult to the database for ensuring unique id, I mean is it a write operation or read operation.

Upvotes: 0

Views: 52

Answers (1)

Gergo Erdosi
Gergo Erdosi

Reputation: 42028

Generation of the ObjectId happens locally, it's not a database operation. 4 values are used to generate the ObjectId:

  • Time: ::Time.new.to_i
  • Machine ID: Digest::MD5.digest(Socket.gethostname).unpack("N")[0]
  • Process ID: Process.pid % 0xFFFF
  • Counter: @counter = (@counter + 1) % 0xFFFFFF

You can find the full code here.

Upvotes: 1

Related Questions