{"record":{"id":"5581a97d364c0ec9","repo":"Automattic/mongoose","slug":"path-path-is-not-in-schema-and-strictquery-is","errorCode":null,"errorMessage":"Path \"${path}\" is not in schema and strictQuery is 'throw'.","messagePattern":"Path \"(.+?)\" is not in schema and strictQuery is 'throw'\\.","errorType":"exception","errorClass":"StrictModeError","httpStatus":null,"severity":"error","filePath":"lib/cast.js","lineNumber":304,"sourceCode":"            _cast(value, numbertype, context);\n            continue;\n          }\n        }\n\n        if (schema.nested[path]) {\n          continue;\n        }\n\n        const strict = 'strict' in options ? options.strict : schema.options.strict;\n        const strictQuery = getStrictQuery(options, schema._userProvidedOptions, schema.options, context);\n        if (options.upsert && strict) {\n          if (strict === 'throw') {\n            throw new StrictModeError(path);\n          }\n          throw new StrictModeError(path, 'Path \"' + path + '\" is not in ' +\n            'schema, strict mode is `true`, and upsert is `true`.');\n        } if (strictQuery === 'throw') {\n          throw new StrictModeError(path, 'Path \"' + path + '\" is not in ' +\n            'schema and strictQuery is \\'throw\\'.');\n        } else if (strictQuery) {\n          delete obj[path];\n        }\n      } else if (val == null) {\n        continue;\n      } else if (utils.isPOJO(val)) {\n        any$conditionals = Object.keys(val).some(isOperator);\n\n        if (!any$conditionals) {\n          obj[path] = schematype.castForQuery(\n            null,\n            val,\n            context\n          );\n        } else {\n          const ks = Object.keys(val);\n          let $cond;","sourceCodeStart":286,"sourceCodeEnd":322,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/cast.js#L286-L322","documentation":"strictQuery controls how mongoose handles query conditions on paths missing from the schema. When it resolves to 'throw', cast() raises StrictModeError naming the path instead of ignoring the condition (plain true would silently delete it). This fires from find/update calls whose filter references an unknown path.","triggerScenarios":"Model.find({ notInSchema: 1 }) with schema or query strictQuery: 'throw'; Model.findOneAndUpdate({ typoField: 'x' }, update) with the same setting; mongoose.set('strictQuery', 'throw') globally combined with user-supplied filters.","commonSituations":"Migrations between mongoose majors where strictQuery defaults changed, so previously-ignored filters start throwing once 'throw' is set; search endpoints forwarding arbitrary filter keys; renamed schema fields while old clients send old names; enabling 'throw' deliberately to catch filter typos.","solutions":["Fix the filter: add the field to the schema or correct the typo","Allow unmodeled filters per query with .setOptions({ strictQuery: false }) or globally with mongoose.set('strictQuery', false)","Whitelist filter keys against Object.keys(Model.schema.paths) before querying"],"exampleFix":"// before\nconst schema = new Schema({ name: String }, { strictQuery: 'throw' });\nawait Model.find({ nam: 'x' }); // typo -> throws\n\n// after\nawait Model.find({ name: 'x' });","handlingStrategy":"validation","validationCode":"function pickKnownFilterKeys(Model, filter) {\n  const out = {};\n  for (const k of Object.keys(filter)) {\n    if (k in Model.schema.paths || k.startsWith('$')) out[k] = filter[k];\n  }\n  return out;\n}\nawait Model.find(pickKnownFilterKeys(Model, req.query));","typeGuard":null,"tryCatchPattern":"try {\n  await Model.find(filter);\n} catch (err) {\n  if (err.name === 'StrictModeError' && /strictQuery/.test(err.message)) {\n    // err.path is the unknown filter field; whitelist it, fix the typo, or relax strictQuery\n  }\n  throw err;\n}","preventionTips":["Map user-supplied filters through an allow-list of modeled fields","Keep strictQuery: 'throw' in dev/test to surface filter typos early","After field renames, grep for old field names in query builders"],"tags":["mongoose","strict-query","schema","query"],"backgroundTag":"strict-mode-violation","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}