ghjunior
ghjunior

Reputation: 119

mongoDB - URL as document ID

Considering I want to create mongoDB documents for a bunch of distinct URLs: what would be the pros and cons (if any) of using the actual URL as the documents _id value instead of the default BSON ObjectId. Thanks in advance!

Cheers, Greg

Upvotes: 4

Views: 2343

Answers (1)

禪 師 無
禪 師 無

Reputation: 422

An overview of the subject here: http://www.mongodb.org/display/DOCS/Object+IDs

It has to be unique, you could potentially put yourself in the position of having to resolve collisions yourself. Better to leave the default _id alone and simply query against a field you're storing in the document, just how God (10gen) intended.

From http://www.mongodb.org/display/DOCS/BSON

The element name "_id" is reserved for use as a primary key id, but you can store anything that is unique in that field. The database expects that drivers will prevent users from creating documents that violate these constraints.

From #mongodb

stupid _id values will probably make querying slow, but that's about it

And another user from #mongodb

Tell him the collisions will result in garbage data

Upvotes: 7

Related Questions