Karan Gaur
Karan Gaur

Reputation: 859

Is searching by _id in mongoDB more efficient?

In my use case, I want to search a document by a given unique string in MongoDB. However, I want my queries to be fast and searching by _id will add some overhead. I want to know if there are any benefits in MongoDB to search a document by _id over any other unique value?
To my knowledge object ID are similar to any other unique value in a document [Point made for the case of searching only].

As for the overhead, you can assume I am caching the string to objectID and the cache is very small and in memory [Almost negligible], though the DB is large.

Upvotes: 3

Views: 4244

Answers (2)

Murat Colyaran
Murat Colyaran

Reputation: 2189

Short answer, yes _id is the primary key and it's indexed. Of course it's fast.

But you can use an index on the other fields too and get more efficient queries.

Upvotes: 2

Ikdemm
Ikdemm

Reputation: 2373

Analyzing your query performance

I advise you to use .explain() provided by mongoDB to analyze your query performance.

Let's say we are trying to execute this query

db.inventory.find( { quantity: { $gte: 100, $lte: 200 } } )

This would be the result of the query execution

{ "_id" : 2, "item" : "f2", "type" : "food", "quantity" : 100 }
{ "_id" : 3, "item" : "p1", "type" : "paper", "quantity" : 200 }
{ "_id" : 4, "item" : "p2", "type" : "paper", "quantity" : 150 }

If we call .execution() this way

db.inventory.find(
   { quantity: { $gte: 100, $lte: 200 } }
).explain("executionStats")

It will return the following result:

{
   "queryPlanner" : {
         "plannerVersion" : 1,
         ...
         "winningPlan" : {
            "stage" : "COLLSCAN",
            ...
         }
   },
   "executionStats" : {
      "executionSuccess" : true,
      "nReturned" : 3,
      "executionTimeMillis" : 0,
      "totalKeysExamined" : 0,
      "totalDocsExamined" : 10,
      "executionStages" : {
         "stage" : "COLLSCAN",
         ...
      },
      ...
   },
   ...
}

More details about this can be found here

How efficient is search by _id and indexes

To answer your question, using indexes is always more efficient. Indexes are special data structures that store a small portion of the collection's data set in an easy to traverse form. With _id being the default index provided by MongoDB, that makes it more efficient.

Without indexes, MongoDB must perform a collection scan, i.e. scan every document in a collection, to select those documents that match the query statement.

So, YES, using indexes like _id is better!

You can also create your own indexes by using createIndex()

db.collection.createIndex( <key and index type specification>, <options> )

Optimize your MongoDB query

In case you want to optimize your query, there are multiple ways to do that.

  • Creating custom indexes to support your queries
  • Limit the Number of Query Results to Reduce Network Demand
db.posts.find().sort( { timestamp : -1 } ).limit(10)
  • Use Projections to Return Only Necessary Data
db.posts.find( {}, { timestamp : 1 , title : 1 , author : 1 , abstract : 1} ).sort( { timestamp : -1 } )
  • Use $hint to Select a Particular Index
db.users.find().hint( { age: 1 } )

Upvotes: 2

Related Questions