The Nuances of Mongoose Select
Developing strategies for more effective use of select/project statements with mongoose.
Selecting and Projecting: SQL vs NoSQL
One of the biggest differences I've noticed when working with document storage compared to relational database is the default behaviour of selecting fields in a query.
In SQL, while the *
operator is always available to select all available fields, it's
an active choice to use it. The user must explicitly choose to select whichever fields are required, with
the option to return all of them.
Contrast this with MongoDB, where the default behaviour is to select the entire document
with any query. To narrow down the results the user must opt in to
selecting their desired fields, rather than be forced to make the choice up front (even if that
choice is to use *
).
While this is a fundamental difference between these two systems, I have never really seen it, or the implications of it, discussed in any detail. I believe that in many situations the decision to have Mongo implicitly return all fields without the user making a selection is harmful and causes more problems than the convenience that it affords.
Keep reading...