since we can’t get explain plan on findOne. How does findOne retrieves records? Is it always based on a CollScan?
or is it still smart enough to use indexes if exist?
since we can’t get explain plan on findOne. How does findOne retrieves records? Is it always based on a CollScan?
or is it still smart enough to use indexes if exist?
Hi @Bluetoba,
findOne(...) is just a wrapper for find(...).limit(1).pretty().
So if find() would use an index, findOne() will too.
You can confirm this by checking if the index was used or not.
That’s a screenshot from MongoDB Compass before my query:
Now I run my query:
db.col.findOne({name:"Max"})
And I refresh in Compass:
Note: I think the usage on the
_idindex comes from Compass, not my
findOne().
Cheers,
Maxime.
Thanks Maxime. It’s good to understand it.
This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.