{"record":{"id":"d5de58c55ba9e53a","repo":"mongodb/node-mongodb-native","slug":"document-must-be-a-valid-javascript-object","errorCode":null,"errorMessage":"Document must be a valid JavaScript object","messagePattern":"Document must be a valid JavaScript object","errorType":"exception","errorClass":"MongoInvalidArgumentError","httpStatus":null,"severity":"error","filePath":"src/operations/update.ts","lineNumber":270,"sourceCode":"      upsertedId:\n        Array.isArray(res.upserted) && res.upserted.length > 0 ? res.upserted[0]._id : null,\n      upsertedCount: Array.isArray(res.upserted) && res.upserted.length ? res.upserted.length : 0,\n      matchedCount: Array.isArray(res.upserted) && res.upserted.length > 0 ? 0 : res.n\n    };\n  }\n}\n\nexport function makeUpdateStatement(\n  filter: Document,\n  update: Document | Document[],\n  options: UpdateOptions & { multi?: boolean } & { sort?: Sort }\n): UpdateStatement {\n  if (filter == null || typeof filter !== 'object') {\n    throw new MongoInvalidArgumentError('Selector must be a valid JavaScript object');\n  }\n\n  if (update == null || typeof update !== 'object') {\n    throw new MongoInvalidArgumentError('Document must be a valid JavaScript object');\n  }\n\n  const op: UpdateStatement = { q: filter, u: update };\n  if (typeof options.upsert === 'boolean') {\n    op.upsert = options.upsert;\n  }\n\n  if (options.multi) {\n    op.multi = options.multi;\n  }\n\n  if (options.hint) {\n    op.hint = options.hint;\n  }\n\n  if (options.arrayFilters) {\n    op.arrayFilters = options.arrayFilters;\n  }","sourceCodeStart":252,"sourceCodeEnd":288,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/operations/update.ts#L252-L288","documentation":"Thrown by makeUpdateStatement() (src/operations/update.ts:270) when the update argument is null or not an object/array. The driver validates the 'u' field of the wire-protocol update statement; it must be either an update document with operators (e.g. $set) or an aggregation pipeline array (for update with pipeline). Surfaces as MongoInvalidArgumentError synchronously.","triggerScenarios":"Calling collection.updateOne({a:1}, null), collection.updateOne({a:1}, 'foo'), or collection.findOneAndUpdate(filter, undefined). Also from bulkWrite with a statement whose update field is not an object.","commonSituations":"Forgetting the update document, passing a JS expression that evaluated to undefined, or confusing the argument order of findOneAndUpdate(filter, update, options).","solutions":["Provide a valid update document (with operators like $set) or an aggregation pipeline array as the second argument.","Verify the variable holding the update document is defined before calling the method.","Double-check the argument order against the method signature."],"exampleFix":"// before\ncollection.updateOne({ a: 1 }, maybeUpdate);\n// after\nconst update = maybeUpdate ?? { $set: { updated: true } };\ncollection.updateOne({ a: 1 }, update);","handlingStrategy":"validation","validationCode":"function assertUpdate(update) {\n  if (update == null || (typeof update !== 'object' && typeof update !== 'object')) {\n    throw new TypeError('update must be an object or array');\n  }\n}\nassertUpdate(myUpdate);\ncollection.updateOne({}, myUpdate);","typeGuard":"function isUpdateDoc(v): v is Record<string, unknown> | unknown[] {\n  return v != null && typeof v === 'object';\n}","tryCatchPattern":"try {\n  await collection.updateOne(filter, update);\n} catch (e) {\n  if (e instanceof MongoInvalidArgumentError && /Document must be/.test(e.message)) {\n    // surface validation error\n  }\n  throw e;\n}","preventionTips":["Always pass an explicit update document; never reuse a variable that could be undefined.","Use TypeScript types (Document | Document[]) for update arguments.","Unit-test the argument order of findOneAndUpdate calls."],"tags":["validation","update","crud","invalid-argument"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}