Skip to content

MongoError: the update operation document must contain atomic operators – How to fix?

How to fix this error when you are working with MongoDB documents. – MongoError: the update operation document must contain atomic operators. ?

Advertisements

I run into this error often even though I know how to update a document in Mongo.

     await db.collection("users")
        .findOneAndUpdate(
            { username: username },
           req.body,
            { new: true, upsert: true, returnOriginal: false }

You should always use $set with the mongo update and findOneAndUpdate operations. Here I want to update the whole document and probably upsert instead of specific values. I forgot $set. Add $set and it works.

Advertisements
     await db.collection("users")
        .findOneAndUpdate(
            { username: username },
            { $set: req.body },
            { new: true, upsert: true, returnOriginal: false }

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.