3 ms·
I'm just starting MongoDB, and I really like it. It's got nice documentation, and a nice culture. But it does have weaknesses, and having just been bitten hard
by wisty 15y ago
I'm just starting MongoDB, and I really like it. It's got nice documentation, and a nice culture.
But it does have weaknesses, and having just been bitten hard by one.
I had a schema something like "db.post.insert({'tags'=['python','mongodb']}).
One tag just couldn't be reliably deleted (via "$pull:{'tags':'immortal tag that will not die'}.
It turns out, I had made "tags" unique, but somehow managed to get a duplicate tag in. (Before the unique index? After? I forget). Trying to delete the tag would "throw" a E11000 error, but since mongodb doesn't check errors it would just result in this immortal tag sitting there. Smirking.
Apparently, using a unique index on an array is the wrong thing to do anyway (does it mean different posts can't share tags? or that you can't have 2 posts with exactly the same set of tags? I'm not sure), and you are just meant to use $addToSet. I'm not sure how you are meant to clean an existing array with duplicate elements though. Perhaps you just have to check in the app layer, then update: db.posts.update({'_id':post['_id']},{'tags':set[post['tags']}.
Lesson 1: if something is failing a lot, or might fail in an annoying way, use db.error() (in Python ... or whatever the last_error command is in your client).
Lesson 2: don't try to use unique indexes on array elements.
Otherwise, it's a very nice db.