Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting the result of an update operation #867

Closed
manast opened this issue Apr 26, 2012 · 15 comments
Closed

Getting the result of an update operation #867

manast opened this issue Apr 26, 2012 · 15 comments

Comments

@manast
Copy link

manast commented Apr 26, 2012

Hello,

According to the documentation in mongodb (http://www.mongodb.org/display/DOCS/Updating#Updating-CheckingtheOutcomeofanUpdateRequest), it is possible to get the result of an update operation. In some cases it is extremely useful to be able to know if the update did update something or not. For instance in my application I want to update all the clients that are observing some document if that document has been changed or not. I can fake this by first getting the document and only updating if necessary, but it is quite inconvenient for several reasons.

Is there any efficient way to get the update result information in mongoose or any good workaround for it?

regards.

@aheckmann
Copy link
Collaborator

right now the number of affected documents is passed to the callback.

Thing.update(query, update, function (err, affected) {
  // affected is the number of docs that were updated
})
  1. i just fixed a bug with this when using strict schemas but it has not been released yet. if you are not using strict schemas the number returned will be correct

  2. the driver now also passes the actual return document but it's not in the latest release of mongoose yet. we'll get that in the next release as well.

@manast
Copy link
Author

manast commented Apr 26, 2012

Thanks for the quick response :).

Ok, I was actually testing with a strict schema and the number returned was 1, which as I understood was the number of elements that matched the query, not the number of elements that actually changed due this update operation.

So If I understood it correctly, that number will be zero if I try to update one document with the same values that it already has, right?

On Apr 26, 2012, at 8:09 PM, Aaron Heckmann wrote:

right now the number of affected documents is passed to the callback.

Thing.update(query, update, function (err, affected) {
 // affected is the number of docs that were updated
})
  1. i just fixed a bug with this when using strict schemas but it has not been released yet. if you are not using strict schemas the number returned will be correct

  2. the driver now also passes the actual return document but it's not in the latest release of mongoose yet. we'll get that in the next release as well.


Reply to this email directly or view it on GitHub:
#867 (comment)

@aheckmann
Copy link
Collaborator

affected is the number of docs updated, even if the new values are identical. this comes straight from mongo.

@manast
Copy link
Author

manast commented Apr 26, 2012

I see. That was what I suspected. It is not really the same thing as I was asking then, as you see from the link I sent above, it should be possible to know from mongo if a document actually was changed by the update or not. Of course if we can get back the updated document after updating, as you mentioned, then we could also check it by hand. It will solve the issue although slightly less optimal than having mongo telling us automatically.

regards.

On Apr 26, 2012, at 9:36 PM, Aaron Heckmann wrote:

affected is the number of docs updated, even if the new values are identical. this comes straight from mongo.


Reply to this email directly or view it on GitHub:
#867 (comment)

@aheckmann
Copy link
Collaborator

in master we have support for the findAndModify command which lets us do an atomic update and return the doc in one op. this will be available in the 3.x release.

@manast
Copy link
Author

manast commented Apr 30, 2012

ok. That will be quite useful, although not so optimal as just getting what really was updated. Specially important in the case of using the $addToSet command: { $addToSet : { a : { $each : [ 3 , 5 , 6 ] } } }

In this case it will be really useful to know what elements actually were added to the set, to notify listeners with that information. Even if it can be done using findAndModify it is slower and also non-atomic.

@aheckmann
Copy link
Collaborator

this isn't currently possible with mongo.

@timoxley
Copy link
Contributor

Related: go watch, vote and/or comment on this issue in hopes that the mongodb team does something about it soon. This has been a major headache for me. https://jira.mongodb.org/browse/SERVER-3409

trying out this fix:

"For the moment, you can add an extra filter to your query condition so
that, if nothing is updated, 'updatedExisting' will be false."

db.foo.update({_id: 'some_id', array: {$ne: 'foo'}}, { $addToSet: { array: 'foo' }})

From: https://groups.google.com/d/topic/mongodb-user/DPW90mH4-0k/discussion

I wonder if there's a way I can get this functionality while still using a mongoose object's array methods? i.e. still being able to use addToSet + model.save(), currently this necessarily requires me to save the object at the time of adding the items to the set.

@aheckmann
Copy link
Collaborator

today we'd need to use foo.update() directly

@madsviktor
Copy link

Is there any update on this? I'm using v. 3.5.3 and it seems like the update callback is still returning the number of affected documents (and not the actual return doc.)

Regards.

@efosao
Copy link

efosao commented Mar 20, 2013

@madsviktor It looks like findOneAndUpdate may do what you're looking for...

http://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate

@madsviktor
Copy link

@efosao Oh, you're so right - findAndModify have been implemented there instead - thanks for the help man :)

@aheckmann
Copy link
Collaborator

The return doc is passed as the third argument:
http://mongoosejs.com/docs/api.html#model_Model.update

On Wednesday, March 20, 2013, madsviktor wrote:

Is there any update on this? I'm using v. 3.5.3 and it seems like the
update callback is still returning the number of affected documents (and
not the actual return doc.)

Regards.


Reply to this email directly or view it on GitHubhttps://github.com//issues/867#issuecomment-15185900
.

Aaron
@aaronheckmann https://twitter.com/#!/aaronheckmann

@aheckmann
Copy link
Collaborator

I meant the details of the operation. For the document itself, yes, use the
findAndModify variants.

On Wednesday, March 20, 2013, Aaron Heckmann wrote:

The return doc is passed as the third argument:
http://mongoosejs.com/docs/api.html#model_Model.update

On Wednesday, March 20, 2013, madsviktor wrote:

Is there any update on this? I'm using v. 3.5.3 and it seems like the
update callback is still returning the number of affected documents (and
not the actual return doc.)

Regards.


Reply to this email directly or view it on GitHubhttps://github.com//issues/867#issuecomment-15185900
.

Aaron
@aaronheckmann https://twitter.com/#!/aaronheckmann

Aaron
@aaronheckmann https://twitter.com/#!/aaronheckmann

@JorgeCeja
Copy link

For future reference:

var query   = { id: 8 }; 
var update  = { title: "new title" }; 
var options = { new: true }; 
MyModel.findOneAndUpdate(query, update, options, function(err, doc){ 
  // Done! 
  // doc.title = "new title" 
});

Credit: https://davidburgos.blog/return-updated-document-mongoose/

@Automattic Automattic locked as resolved and limited conversation to collaborators Jan 21, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants