Mongoose Model.update not working in my code Mongoose Model.update not working in my code mongoose mongoose

Mongoose Model.update not working in my code


I don't see any issues in the code and here the query and syntax are correct, according to the MongoDB update doc.

The update() method returns a WriteResult object that contains the status of the operation. Upon success, the WriteResult object contains the number of documents that matched the query condition, the number of documents inserted by the update, and the number of documents modified.

Here no documents have been modified and the only reason is, you are setting the same data to update in the query.


Try using

isPublished: {    type: Boolean,    default: false}

instead of

isPublished: Boolean


The return {ok: 1, nModified: 0, n: 0} says that the operation was successful but couldn't find, nor update any entry, so that means it's not even matching.

I'm not entirely sure, but I think mongoose only finds ID by ObjectId and not by string.

So you should get the desired outcome by converting it, like that:

const result = await Course.update({ _id: mongoose.Types.ObjectId(id) }, {     $set: { author: 'New', isPublished: false }});