Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 512 Vote(s) - 3.42 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Check if MongoDB upsert did an insert or an update

#1
I can't find this in the documentation in any of the obvious places.
I'd like to know if it is possible to know if Mongo executed an insert or update in the upsert operation?
Reply

#2
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:

[To see links please register here]

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`
Reply

#3
For reference only, in node.js:

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

#4
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)
Reply

#5
The Answer was taken from "MongoDB Applied Design Patterns" Book
![determine whether an upsert was an insert
or an update][1]


[1]:
Reply

#6
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

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through