flâneur — a map of the web's best reading

Mongoose v6.6.7: Mongoose Tutorials: Query Casting

mongoosejs.com · 649 words · saved by 1 readers

The first parameter to Model.find(), Query#find(), Model.findOne(), etc. is called filter. In older content this parameter is sometimes called query or conditions. For example: When you execute the query using Query#exec() or Query#then(), Mongoose casts the filter to match your schema. If Mongoose fails to cast the filter to your schema, your query will throw a CastError. By default, Mongoose does not cast filter properties that aren't in your schema. You can configure this behavior using the strictQuery option for schemas. This option is analogous to the strict option. Setting strictQuery to true removes non-schema properties from the filter: To make Mongoose throw an error if your filter has a property that isn't in the schema, set strictQuery to 'throw': Because of schemas, Mongoose knows what types fields should be, so it can provide some neat syntactic sugar. For example, if you forget to put $in on a non-array field, Mongoose will add $in for you.

Query Casting The first parameter to Model.find() , Query#find() , Model.findOne() , etc. is called filter . In older content this parameter is sometimes called query or conditions . For example: const query = Character . find ({ name : 'Jean-Luc Picard' }); query. getFilter (); // `{ name: 'Jean-Luc Picard' }` // Subsequent chained calls merge new properties into the filter query. find ({ age : { $gt : 50 } }); query. getFilter (); // `{ name: 'Jean-Luc Picard', age: { $gt: 50 } }` When you execute the query using Query#exec() or Query#then() , Mongoose casts the filter to match your schema.

Explore this link on the map →

related reading