Why does upsert a record using update_one raise ValueError?

This is because you didn’t specify any update operator.
For example to $set the id value use:

db.collection.update_one({"_id":"key1"}, {"$set": {"id":"key1"}}, upsert=True)

Note that in the Mongo shell, this will simply replace the document with the new document.

Leave a Comment