Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDb: Benefit of using ObjectID vs a string containing an Id?

Tags:

php

mongodb

Is there any benefit to storing an id to a related document as an ObjectId versus storing it as a string literal?

Using ObjectID:

{
   "_id": ObjectId("522bb79455449d881b004d27"),
   "username": "admin",
   "folder": ObjectId("522bb79455449d881b004d23")
}

versus a string:

{
   "_id": ObjectId("522bb79455449d881b004d27"),
   "username": "admin",
   "folder": "522bb79455449d881b004d23"
}

For my API where I'm sending data back to a client... using the string means I don't have to "cleanup" the data... and as we have to do a second query to get the folder document anyway... is it worth using ObjectId? (and if so why?)

Thanks

like image 278
Sunil Avatar asked Sep 08 '13 04:09

Sunil


People also ask

Should I use MongoDB ObjectID?

You should NOT convert the ObjectId in the database to a string, and then compare it to another string. If you'd do this, MongoDB cannot use the _id index and it'll have to scan all the documents, resulting in poor query performance. Show activity on this post. Don't.

What is the use of ObjectID in MongoDB?

MongoDB uses ObjectIds as the default value of _id field of each document, which is generated while the creation of any document. The complex combination of ObjectId makes all the _id fields unique.

Is MongoDB ObjectID unique?

The science behind MongoDB object IDs MongoDB is a NoSQL database that operates with collections and documents. Each document created on MongoDB has a unique object ID property. So when creating a document without entering an ID, the document will be created with an auto-generated ID.

How does MongoDB compare ObjectID?

Mongoose uses the mongodb-native driver, which uses the custom ObjectID type. You can compare ObjectIDs with the . equals() method. With your example, results.


3 Answers

The biggest reason is that ObjectIDs are 12 bytes, whereas an equivalent string is 24 bytes. Over a large enough collection, those 12 bytes saved per ID really add up! Those IDs also mean fewer bytes transferred over the wire when reading or writing the document, as well.

Additionally, some ODMs expect ObjectIDs for external document references, and may be confused by string versions of the ID. I am not familiar enough with PHP ODMs to say if this might affect you specifically, though.

Regarding the API stuff, though, you should probably be doing normalization of the data before sending it to the client anyhow, because since Mongo doesn't enforce a schema, you can have literally any sort of data in a given field, so you might have some documents that have string IDs, and others that have BSON IDs, and your API would happily send them both through to the client, but one or the other might cause breakage. In this particular case, you should use BSON ObjectIDs in your documents, and then should cast them to strings in your API output.

like image 140
Chris Heald Avatar answered Oct 18 '22 20:10

Chris Heald


Briefly, for example, if you shorten the filed named last_name to lname , you could save 9 bytes per document. This really makes a difference if you have millions of documents in your collection.

like image 22
maow Avatar answered Oct 18 '22 18:10

maow


In addition, ObjectId() has the following attribute and methods that you can use.

  1. str - Returns the hexadecimal string representation of the object. as a Date.

  2. ObjectId.toString() # Returns the JavaScript representation.

  3. ObjectId.getTimestamp() # Returns the timestamp portion of the object

  4. ObjectId.valueOf() # Returns the representation of the object as a hexadecimal string

like image 36
Ahmad Sharif Avatar answered Oct 18 '22 19:10

Ahmad Sharif