deno.land / x / mongoose@6.7.5 / docs / tutorials / query_casting.md

query_casting.md

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:

[require:Cast Tutorial.*get and set]

When you execute the query using Query#exec() or Query#then(), Mongoose casts the filter to match your schema.

[require:Cast Tutorial.*cast values]

If Mongoose fails to cast the filter to your schema, your query will throw a CastError.

[require:Cast Tutorial.*cast error]

The strictQuery Option

By default, Mongoose does not cast filter properties that aren't in your schema.

[require:Cast Tutorial.*not in 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:

[require:Cast Tutorial.*strictQuery true]

To make Mongoose throw an error if your filter has a property that isn't in the schema, set strictQuery to 'throw':

[require:Cast Tutorial.*strictQuery throw]

Implicit $in

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.

[require:Cast Tutorial.*implicit in]
mongoose

Version Info

Tagged at
a year ago