jennifer
jennifer

Reputation: 762

How many documents does updateOne effect?

It would seem that it would simply provide the Update method in the overall CRUD ( Create, Read, Update, Delete. )

However the docs don't seem to make sense ( Mongoose - updateOne ):

const res = await Person.updateOne({ name: 'Jean-Luc Picard' }, { ship: 'USS Enterprise' });
res.n; // Number of documents matched
res.nModified; // Number of documents modified

Why is it returning some parameters that count the number of documents matched and modified?

Does it update one or does it update more than one?

Also, what does param1 and param2 refer to in

const res = await Person.updateOne(param1, param2);

The reference I posted above causes more confusion than help.

Upvotes: 0

Views: 160

Answers (1)

thammada.ts
thammada.ts

Reputation: 5245

updateOne, as the name suggests, can update up to one document.

It's returning n and nModified because the that's what the Node.js MongoDB Driver API returns for several update operations (updateOne, updateMany, replaceOne)

param1 is the filter that you use to query the document(s) to be updated. param2 is the change you want to apply for the matched document(s)

n "Number of documents matched", means the number of documents that matches the filter, provided as param1, for updateOne it can be 0 or 1

nModified "Number of documents modified", means the number of documents that matches the filter and was actually modified because previous value did not match what's given in param2, for updateOne it can be 0 or 1 (generally less than or equal to n)

see also https://docs.mongodb.com/manual/reference/method/db.collection.updateOne/

Upvotes: 2

Related Questions