Written by Pete Corey on Jan 26, 2015.

My most recent project, Suffixer, involves doing a primarily text-based search over more than 80k documents. Initially, I was using $regex to do all of my querying, but this approach was unacceptably slow. I decided to try out MongoDB’s text search functionality to see if I could get any performance gains.

I replaced my main query with something like this:

MyCollection.find({$text: {$search: searchText}});

Unfortunately, Meteor seemed very unhappy with this change. I immediately began setting errors in my server logs:

Exception from sub ZskAqGy2t2jJckpXK MongoError: invalid operator: $search

A quick investigation showed what was wrong. Meteor uses Mongo 2.4, instead of 2.6. You can check this by running db.version() in your Mongo shell (meteor mongo). Text search in 2.4 is syntactically significatly different than text search in 2.6.

If you insist on using Meteor’s bundled version of Mongo, this Meteorpedia post shows how to manually kick off the search command in a reactive context.

A much better solution is to simply use your own instance of Mongo 2.6. Follow the available installation guides to get an instance running on your machine (or remotely). Once Mongo is successfully installed, you can instruct Meteor to use this new instance of Mongo by pointing to it with the MONGO_URL environment variable.

Using Mongo’s text search coupled with a text index drastically improved the performance of my web-app.