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
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.
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.
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.
Mongoose uses the mongodb-native driver, which uses the custom ObjectID type. You can compare ObjectIDs with the . equals() method. With your example, results.
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.
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.
In addition, ObjectId() has the following attribute and methods that you can use.
str
- Returns the hexadecimal string representation of the object.
as a Date.
ObjectId.toString()
# Returns the JavaScript representation.
ObjectId.getTimestamp()
# Returns the timestamp portion of the object
ObjectId.valueOf()
# Returns the representation of the object as a hexadecimal string
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With