Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if MongoDB upsert did an insert or an update

Tags:

mongodb

People also ask

What is the difference between update and Upsert in MongoDB?

In MongoDB, upsert is an option that is used for update operation e.g. update(), findAndModify(), etc. Or in other words, upsert is a combination of update and insert (update + insert = upsert).

Is there an Upsert option in the MongoDB insert command?

insert() provides no upsert possibility.

Does MongoDB update insert if not exists?

ASP.NET Core 3 MVC Application with MongoDB You can use upsert i.e. whenever you insert a value and it already exist then update would be performed. If the value does not already exist then it would get inserted.

How does Upsert work in MongoDB?

Here in MongoDB, the upsert option is a Boolean value. Suppose the value is true and the documents match the specified query filter. In that case, the applied update operation will update the documents. If the value is true and no documents match the condition, this option inserts a new document into the collection.


Yes there is, on a safe call (or getLastError) the update function will return an array with an upsert field and a updatedExisting field.

You can read the PHP version of this here: http://php.net/manual/en/mongocollection.insert.php towards the bottom.

As it says within the documentation on upserted:

If an upsert occured, this field will contain the new record's _id field. For upserts, either this field or updatedExisting will be present (unless an error occurred).

So upserted contains the _id of the new record if a insert was done or it will increment updatedExisting if it updated a record.

I am sure a similar thing appears in all drivers.

Edit

It will actually be a boolean in the updatedExisting field of true or false


For reference only, in node.js:

collection.update( source, target, { upsert: true }, function(err, result, upserted) {
  ...
});

For reference only, in node.js using Mongoose 3.6:

model.update( findquery, updatequery, { upsert: true }, function(err, numberAffected, rawResponse) {
  ...
});

Where rawResponse looks like this when it has updated an existing document:

{ updatedExisting: true,
  n: 1,
  connectionId: 222,
  err: null,
  ok: 1 }

And it looks like this when it has created a new document:

{ updatedExisting: false,
  upserted: 51eebc080eb3e2208a630d8e,
  n: 1,
  connectionId: 222,
  err: null,

(Both cases would return numberAffected = 1)


The Answer was taken from "MongoDB Applied Design Patterns" Book !determine whether an upsert was an insert or an update


Using MongoDB driver 3.5.9 under Node.js, I found that there are these properties we are interested in after using updateOne with { upsert: true }:

{
  modifiedCount: 0,
  upsertedId: null,
  upsertedCount: 0,
  matchedCount: 1
}

When upsert inserted something, we will get upsertedCount > 0 and upsertedId will hold the newly inserted document ID. When upsert modified something, we will get modifiedCount > 0.

The tutorial for all CRUD operations is here https://mongodb.github.io/node-mongodb-native/3.6/tutorials/crud/