Repository navigation
Conversation
… parent schema Arrays whose items have no schema ([`Mixed`], [`String`], `Array`, ...) have no embedded schema to cast $or/$and/$nor clauses against. The fallback used the parent document schema, so with strictQuery enabled Mongoose stripped the clause fields, cast fields that happened to share a name with a parent path, and bulkWrite() threw because there is no query context. Leave these clauses as-is, like Mongoose 6.x did. Clauses in document arrays are still cast against the subdocument schema.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
$or,$and, and$norclauses inside$elemMatchon an array whose items have no schema ([Mixed],[String],Array, ...) are currently cast against the parent document schema. WithstrictQueryenabled this strips the clause fields ($or: [{}, {}]), casts fields that happen to share a name with a parent path (which can throw), andbulkWrite()throwsTypeError: Cannot read properties of undefined (reading 'schema')because there is no query context.These arrays have no item schema, so their
$elemMatchclauses are arbitrary objects rather than subdocument paths. This change leaves the clauses as-is, matching Mongoose 6.x behavior. Document arrays still cast clause fields against their subdocument schema.Fixes #16754.
Examples
Tests
Added regression tests for
$or/$and/$noron[Mixed],[String], and bareArraypaths, for document arrays (still cast), and forbulkWrite()filter casting.npx mocha --exit test/model.query.casting.test.js— 54 passingnpx mocha --exit test/query.test.js test/model.query.casting.test.js— 404 passingnpx mocha --exit test/model.test.js --grep bulkWrite— 45 passing