Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does MongoDB fetch entire document even if single field is projected?

I have a MongoDB Collection for weather data with each document consisting about 50 different weather parameters fields. Simple Example below:

{
"wind":7,
"swell":6,
"temp":32,
...
"50th_field":32
}

If I only need one field from all documents, say temp, my query would be this:

db.weather.find({},{ temp: 1})

So internally, does MongoDB has to fetch the entire document for just 1 field which was requested(projected)? Wouldn't it be an expensive operation?

I tried MongoDB Compass to benchmark timings, but the time required was <1ms so couldn't figure out.

like image 819
Prasad Ostwal Avatar asked Oct 26 '25 17:10

Prasad Ostwal


1 Answers

MonogDB will read all data, however only field temp (and _id) will be transmitted over your network to the client. In case your document are rather big, then the over all performance should be better when you project only the fields you need to get.

like image 178
Wernfried Domscheit Avatar answered Oct 28 '25 07:10

Wernfried Domscheit