Quantcast
Channel: Active questions tagged mongodb-atlas - Stack Overflow
Viewing all articles
Browse latest Browse all 271

Why doesn't Mongoose's findById() method increment _id_ index usage in Mongo?

$
0
0

I have a collection where records are typically looked up directly by their _id property. This is done thousands of times per day by the backend application using Mongoose's findById() method.

When I look at the usage for the _id_ index on the collection automatically created by Mongo, it only reports 60 uses in the past ~2 weeks. This is much lower than I would expect relative to how frequently records in the collection are read this way. Furthermore, I have confirmed that the frontend workflow that triggers the findById() calls does not increment the index usage by comparing the usage across calls that I'm making personally.

Interestingly, running a query of the form { _id: UUID("<uuid>") } from the Compass GUI does increment the index usage.

I'm curious why the findById() calls don't increment the index usage. If those calls aren't leveraging the index, what is the mechanism that they're using to look up the records? Is it possible that I have some kind of schema misconfiguration that is resulting in collection scans to find records by their _id?

A few explanations I've considered and ruled out

  • There is no caching layer in the stack (aside from automatic DB caching...?) that would be intercepting these requests before they make it to the database
  • The documentation suggests that the primary purpose of this index is to ensure uniqueness for the _id_ property. So it occurred to me that perhaps this index isn't used for lookups. But this doesn't make sense because
    • Compass uses it for queries submitted there as I mentioned above
    • _id_ indices for other collections in the instance have much higher usage

Other potentially relevant information

  • The backend is nestjs
  • The database is a MongoDB Atlas M10 instance

Viewing all articles
Browse latest Browse all 271

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>