Automattic/mongoose · error · Error

Could not find path "${filterPath}" in schema

Error message

Could not find path "${filterPath}" in schema

What it means

When strictQuery is enabled (true or 'throw'), Mongoose requires every path referenced inside arrayFilters to exist in the schema; unknown paths throw this Error during update casting instead of being passed through to MongoDB. strictQuery true and 'throw' are deliberately equivalent here because silently stripping an array filter would change which elements the update touches.

Source

Thrown at lib/helpers/update/castArrayFilters.js:104

      } else {
        // If there are multiple array filters in the path being updated, make sure
        // to replace them so we can get the schema path.
        filterPathRelativeToBase = cleanPositionalOperators(filterPathRelativeToBase);
        schematype = getPath(filterBaseSchema, filterPathRelativeToBase, discriminatorValueMap);
      }

      if (schematype == null) {
        if (!strictQuery) {
          return;
        }
        const filterPath = filterPathRelativeToBase == null ?
          baseFilterPath + '.0' :
          baseFilterPath + '.0' + filterPathRelativeToBase;
        // For now, treat `strictQuery = true` and `strictQuery = 'throw'` as
        // equivalent for casting array filters. `strictQuery = true` doesn't
        // quite work in this context because we never want to silently strip out
        // array filters, even if the path isn't in the schema.
        throw new Error(`Could not find path "${filterPath}" in schema`);
      }
      if (typeof filter[key] === 'object') {
        filter[key] = castFilterPath(query, schematype, filter[key]);
      } else {
        filter[key] = schematype.castForQuery(null, filter[key]);
      }
    }
  }
}

View on GitHub (pinned to 49cdab0136)

Solutions

  1. Add the missing path to the (sub)document schema
  2. Fix the typo in the array filter path so it matches a schema path
  3. Relax strict query mode for this update: pass { strictQuery: false } in the update options (or set strictQuery: false on the schema)

Example fix

// before
await Model.updateOne({}, { $set: { 'items.$[e].status': 2 } },
  { strictQuery: true, arrayFilters: [{ 'e.extraField': true }] }); // extraField not in schema

// after
const schema = new Schema({ items: [{ status: Number, extraField: Boolean }] });
await Model.updateOne({}, { $set: { 'items.$[e].status': 2 } },
  { strictQuery: true, arrayFilters: [{ 'e.extraField': true }] });
Defensive patterns

Strategy: validation

Validate before calling

// Check every array-filter path exists in the schema before updating
function arrayFilterPathsExist(schema, filters) {
  for (const f of filters) {
    for (const p of Object.keys(f)) {
      const sub = p.split('.').slice(1).join('.'); // drop identifier prefix like 'e.'
      if (sub && !schema.path(`items.0.${sub}`) && !schema.path(`items.${sub}`)) return false;
    }
  }
  return true;
}

Try / catch

try {
  await Model.updateOne(f, u, { strictQuery: true, arrayFilters });
} catch (err) {
  if (/Could not find path .* in schema/.test(err.message)) {
    // add the path to the schema or re-run with { strictQuery: false }
  } else throw err;
}

Prevention

When it happens

Trigger: Model.updateOne({}, { $set: { 'items.$[e].status': 2 } }, { arrayFilters: [{ 'e.someField.notInSchema': true }]) with strictQuery: true (query option, schema option, or inherited from a strict schema) where items.someField is not defined in the schema.

Common situations: Collections holding fields that exist in MongoDB but were never declared in the Mongoose schema (schema drift); enabling strictQuery globally for security and breaking older queries; typos in the sub-paths of array filter identifiers.

Related errors


AI-assisted analysis of Automattic/mongoose@49cdab0136 (2026-08-21). Data as JSON: /api/errors/d49ca5385f868c6a. Report an issue: GitHub.