Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongo $in operator performance

Tags:

mongodb

Is it slow/poor form to use the $in operator in MongoDB with a large array of possibilities?

posts.find({     author : {         $in : ['friend1','friend2','friend3'....'friend40']      } }) 

App Engine, for example, won't let you use more than 30 because they translate directly to one query per item in the IN array, and so instead force you into using their method for handling fan out. While that's probably the most efficient method in Mongo too, the code for it is significantly more complex so I'd prefer to just use this generic method.

Will Mongo execute these $in queries efficiently for reasonable-sized datasets?

like image 687
Derek Dahmer Avatar asked Feb 10 '11 09:02

Derek Dahmer


People also ask

Does MongoDB Sharding improve performance?

Sharded clusters in MongoDB are another way to potentially improve performance. Like replication, sharding is a way to distribute large data sets across multiple servers. Using what's called a shard key, developers can copy pieces of data (or “shards”) across multiple servers.

What does $IN do in MongoDB?

For comparison of different BSON type values, see the specified BSON comparison order. If the field holds an array, then the $in operator selects the documents whose field holds an array that contains at least one element that matches a value in the specified array (for example, <value1> , <value2> , and so on).

How fast is MongoDB search?

By default, MongoDB records all queries which take longer than 100 milliseconds.


1 Answers

It can be fairly efficient with small lists (hard to say what small is, but at least into the tens/hundreds) for $in. It does not work like app-engine since mongodb has actual btree indexes and isn't a column store like bigtable.

With $in it will skip around in the index to find the matching documents, or walk through the whole collection if there isn't an index to use.

like image 181
Scott Hernandez Avatar answered Sep 22 '22 10:09

Scott Hernandez