Two basic questions regarding mongoDb default indexes (performance and usage)

  1. When using the default index ("_id" field) in a query, does the collection perform a comparison of values with every document until it matches that id?
    Or is it stored in a way that it references a slot in memory (to provide direct access)?

  2. Do these two behave exacly the same?:

   A) SomeCollection.findOne({_id:'du4hgyeurn'})

   B) SomeCollection.findOne('du4hgyeurn')
  1. As with all indexes, they are stored to allow efficient lookup - I’m not sure off the top of my head what this is, but I’d guess at O(log n) o(1)

  2. Yes, this is identical, its a meteor wrapper that says something like if (typeof selector === "string") selector = { _id: selector }

3 Likes

Thanks!
May I please get an ELI5 on that?

they are stored to allow efficient lookup

Does that mean that it doesn’t check for a match with every document? Or that it uses a special algorithm (like a tree data structure, that would jump between branches to match every character)?

https://docs.mongodb.com/manual/indexes/

3 Likes