{"record":{"id":"db20d322bd96d965","repo":"Automattic/mongoose","slug":"path-path-is-not-in-the-schema","errorCode":null,"errorMessage":"Path `${path}` is not in the schema","messagePattern":"Path `(.+?)` is not in the schema","errorType":"exception","errorClass":"MongooseError","httpStatus":null,"severity":"error","filePath":"lib/schema.js","lineNumber":543,"sourceCode":"      'got \"' + typeof paths + '\"');\n  }\n\n  for (const path of paths) {\n    if (this._hasEncryptedField(path)) {\n      const encrypt = this.encryptedFields[path];\n      const schemaType = this.path(path);\n      newSchema.add({\n        [path]: {\n          encrypt,\n          [this.options.typeKey]: schemaType\n        }\n      });\n    } else if (this.nested[path]) {\n      newSchema.add({ [path]: get(this.tree, path) });\n    } else {\n      const schematype = this.path(path);\n      if (schematype == null) {\n        throw new MongooseError('Path `' + path + '` is not in the schema');\n      }\n      newSchema.add({ [path]: schematype });\n    }\n  }\n\n  if (!this._hasEncryptedFields()) {\n    newSchema.options.encryptionType = null;\n  }\n\n  return newSchema;\n};\n\n/**\n * Returns a new schema that has the `paths` from the original schema, minus the omitted ones.\n *\n * This method is analagous to [Lodash's `omit()` function](https://lodash.com/docs/#omit) for Mongoose schemas.\n *\n * #### Example:","sourceCodeStart":525,"sourceCodeEnd":561,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/schema.js#L525-L561","documentation":"While copying paths, Schema.prototype.pick() looks each requested path up via this.path(path); if the schematype is null (and the path is not nested and not an encrypted field), Mongoose throws 'Path `x` is not in the schema'. The input list must name existing top-level or nested paths exactly — there is no wildcard or silent skip.","triggerScenarios":"`schema.pick(['nonexistent'])`; casing/spelling mismatches ('Name' vs 'name'); picking dotted paths that do not exist; picking paths that were removed via schema.remove(); deriving the pick list from user input or a stale constant.","commonSituations":"Field lists kept in a separate constant that drifts from the schema after renames; generating pick lists from API allowlists without syncing to schema changes; picking subdocument paths after a schema refactor.","solutions":["Fix the list to contain only real paths: verify with `schema.path('name')` or `Object.keys(schema.paths)`.","Filter dynamically sourced lists against existing paths before calling pick.","After renames, update every pick list; add a unit test that calls pick() on your production list."],"exampleFix":"// before\nconst sub = schema.pick(['name', 'nickname']); // throws if 'nickname' is not defined\n\n// after\nconst wanted = ['name', 'nickname'].filter(p => schema.path(p) != null);\nconst sub = schema.pick(wanted);","handlingStrategy":"validation","validationCode":"function pickExisting(schema, wanted) {\n  const valid = wanted.filter(p => schema.path(p) != null || schema.nested[p]);\n  return schema.pick(valid);\n}","typeGuard":"const pathExists = (schema, p) => schema.path(p) != null || Boolean(schema.nested[p]);","tryCatchPattern":"try { return schema.pick(wanted); } catch (err) { if (err instanceof mongoose.Error && /is not in the schema/.test(err.message)) { return schema.pick(wanted.filter(p => schema.path(p) != null)); } throw err; }","preventionTips":["Derive pick lists from Object.keys(schema.paths) or validate against it.","Sync field-list constants after schema renames.","Add a unit test that runs your production pick list."],"tags":["mongoose","schema","pick","unknown-path"],"backgroundTag":"unknown-schema-path","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}