What is the explain plan behind findOne

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 _id index comes from Compass :smiley:, not my findOne().

Cheers,
Maxime.

3 Likes

Thanks Maxime. It’s good to understand it.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.