MongoDB Indexing

I am using Ruby and MongoDB in a project and I got today this Exception here:

Mongo::OperationFailure (too much data for sort() with no index):

The Problem was really that the searched field was not indexed. You can add an index like this:

db.products.ensureIndex({name:1});

That means the attribute “name” on the “products” document get indexed in asc order. That improved my search results enorm. But if the search results are to big, you will still get the same error.

I just solved the problem by limiting the search results. If you are using MongoID for ruby you can add this to your query:

.limit(300)

That means you will just get the first 300 search results. In my case that is perfect.

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