How fast is it to look up by ObjectId in Mongodb? How fast is it to look up by ObjectId in Mongodb? mongodb mongodb

How fast is it to look up by ObjectId in Mongodb?


A more precise answer: MongoDB uses B-Tree indexes. Searching for a particular value in a B-Tree has O(log n) complexity in the average and worst case, which can be considered reasonably fast (i.e. a binary search). It is not constant complexity = O(1) though, so you still might have some slowdown effects if the index size grows larger than available RAM. (MongoDB tries to keep the indexes in RAM, and every IO needed to look up an index on disk will slow down your query considerably).


_id is the primary key. It's indexed. Of course it's fast.


ObjectIds, if your primary method of data access, will be the fastest way to retrieve your stuff from MongoDB. We utilize our MongoDB as a keyed repository for most of our data access. You'll have great results doing what you're doing.