MongoDb _id field

What the Mongo doc says about it is:

_id Field
If the document does not specify an _id field, then MongoDB will add the _id field and assign a unique ObjectId for the document before inserting. Most drivers create an ObjectId and insert the _id field, but the mongod will create and populate the _id if the driver or application does not.

http://docs.mongodb.org/manual/reference/method/db.collection.insert/

And:

ObjectId is a 12-byte BSON type, constructed using:

a 4-byte value representing the seconds since the Unix epoch,
a 3-byte machine identifier,
a 2-byte process id, and
a 3-byte counter, starting with a random value.

In our project, we leave _id to be auto-generated and add autoincremented fields to our entities where we want unique identity field. So, mainly we just don’t mind the _id. Maybe it’s wrong but that is the strategy that’s been selected from the very beginning, and now it’s history. But sometimes the _id reminds about itself in a painful way.

When we add new clients to the system, we have some initial setup done, some objects precreated for them to be able to start working with as little stress as possible. Recently we added some objects to that initial setup that were alike for each client, and when I worked on the implementation I decided that it was best to keep those objects in a separate collection for easy updates and just clone them from there for every newly created client. So it’s been done: the objects were looked up from that collection, we added a unique autoincrement, client id, saved to another collection and were done with it. But testing it I found out that every new client creation process led to those objects being gone from the previous client.

They just disappeared.
No sign of them at all.
But where did they go?
I checked the oplogs. No delete operations on that collection at that point in time. None.
Of course it’s easy to see now that it was probably the simplest and stupidest mistake in working with mongodb objects, but it took me a couple of hours to figure out the reason for that error.

When we took objects for cloning from that initial collection, they already had _id fields, autogenerated by mongodb. When we cloned, we overrode our own incremental id fields, but the _id stayed. And with each new client the previous client objects were simply overwritten.

It took a one-liner fix to set _id to null after cloning the object.
Duh.

Advertisements

About Maryna Cherniavska

I have productively spent 10+ years in IT industry, designing, developing, building and deploying desktop and web applications, designing database structures and otherwise proving that females have a place among software developers. And this is a good place.
This entry was posted in mongodb and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s