Posts Tagged MongoId

Mongoid, db.system.namespaces queries

Recently I faced some issues with Mongoid when upgrading my Rails app from REE+Passenger to MRI 1.9.3+Unicorn.

There are some Resque workers in the background. After some deploy they started to consume a ton of traffic from MongoDB. After some investigation, I found that they heavily read system.namespaces collection. I tried upgrading to latest versions of mongoid(2.4.3) and mongo(1.5.2) to no avail. This does not happen with normal unicorn workers. This also does not happen if I downgrade mongoid to 2.0.1.

I am still not sure what’s happening here. I’ll update this post when I discover something.

Tags: ,

MongoId and atomic increment

I am starting to learn this new piece of technology, the MongoDB. It has many sweet features. However, using Ruby driver directly is not too comfortable. Plus we’re all hooked to ORM sweeteners. So people started developing ORM libraries for MongoDB and Ruby (MongoId, MongoMapper to name a few).

I have decided to try MongoId first. It seems to be a quite good library. However, it still has gaps in documentation. Today I was finding out how to perform an atomic increment of a field. In SQL world this would look like:

UPDATE users SET spam_count = spam_count + 1;

Mongo Ruby driver supports this kind of operation, but MongoId doesn’t. So I was trying to get down to the driver. And this is what I couldn’t easily find in the docs. If you’re like me, I’ll save you a couple of hours. Here it is:

# assume we know object id already
User.collection.update({'_id' => '4c05848e45760182b5000001'}, {'$inc' => {'spam_count' => 1}})

Tags: ,