MongoDB Query Performance: return all vs select fields

I have unexpected results comparing the performance of various queries in a collection that I made for testing. The collection somewhat imitates my real needs with 10,000 documents, each with 20 fields (each with 5-30 characters). All documents are the same and only have _id different (maybe this is somehow a problem?).

Unlike the official version of MongoDB, indicating which fields to return do not lead to better performance, but much worse.

A typical find is performed in approximately 5 ms.

 db.collection.find().explain() 

User find is performed in approximately 30 ms.

 db.collection.find({},{Field1:1,Field2:1,Field3:1,Field4:1,Field5:1,Field6:1,Field7:1},{}).explain() 

Is the simple query “find everything” and “return everything” faster or am I missing something?

+6
source share
1 answer

If you return the whole document, this is less than the overhead for the database, because it does not convert the document to return partial. All documents are stored in a database in BSON format. And he returned in the same way.

In your case, a small overhead is expected.

The field limitation is suitable for large amounts of data, when you have 10,000 resulting documents, and it is much faster to convert a document at the DBMS level, rather than transferring through the socket level.

+1
source

All Articles