lolops
lolops

Reputation: 107

Performance difference when using find+projection vs select

Is there a difference or performance penalty when I use db.collection.find('stuff'{projection}) vs db.collection.find('stuff').select({'keyWeWant'})? I've struggled with documentation for a while now and can't find anything useful.

Upvotes: 1

Views: 1822

Answers (2)

DBC
DBC

Reputation: 41

Can you explain which language you are using because the syntax

db.collection.find('stuff').select({'keyWeWant'})

Is something I don't know.

For your question : Using projection wil only return the fields included in the projection so you will save network. You can utilize this even further and create a so called covered-index, that holds the query keys and projection keys and so you get your data directly from the index which will be a serious performance gain. Offcourse, adding to much data to your index will hurt the performance on that side so choose your battle wisely

Regards Chris

Upvotes: 0

Rohit Jain
Rohit Jain

Reputation: 2092

I would suggest to check $explain, you will find the actual query execution. I believe internally it will same

https://docs.mongodb.org/manual/reference/operator/meta/explain/

Upvotes: 3

Related Questions