-
Notifications
You must be signed in to change notification settings - Fork 260
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
updateOne update property with same value doesn't return nModified = 1 #84
Comments
The write results fields in each driver are coming directly from the database, so this is a question better suited to the mongodb-user list than a driver project.
I don't follow why the current result is an issue. You say that you need to return whether the document is actually modified, which |
I base the operation on being a success if nModified = 1. If the user updates with the same value, it returns nModified = 0 and isn't a success, and returns an error to the front end. This only occurs if the end user updates a property with the same value. My only workaround is to test if the end user is trying to update the value of the document with the current value, which is kind of a pain. I'm just looking for a solution to get a success whether the value is the same or different, and not create a special case if the value is the same. |
There are three possibilities:
If I understand correctly, you don't care if
Earlier you said "avoid checking if the user is trying to update with the same value (which has no incidence)". What did you mean by "no incidence" there. I took that to mean such a scenario does not occur. Alternatively, you could incorporate the value you expect to |
Thank you for your comment, I will use nMatched = 1 and will consider that if the document is matched, the update must have had occurred properly. |
My updateOne() code below does in fact update the mongo document, but nModified is always returned as "null". updateOne() is "New in version 3.2." and I'm still using version 2.4.6, so maybe that is part of the issue? Seems like I should still be getting nModified = 1 and be able to get this report from $response->getModifiedCount(). The other issue I wanted to point out is: with the legacy mongo driver, on an update I could simply look for $response['ok']. It was unclear from documentation that I needed to call either $response->isAcknowledged(), or $response->getModifiedCount() to get information back from the response. updateOne() code:
Updated document:
Environment |
In the future, please open a new issue instead of replying to an existing, closed issue. Otherwise, it's quite possible that your reply might go unnoticed.
The nModified field is only supported via the write commands in MongoDB 2.6+. There is no equivalent field to check in older versions of the server. Typically, you would access
The UpdateResult class in the library wraps the extension's WriteResult class; however, I noticed that the PHP.net documentation for that class is incomplete. I've opened PHPC-653 to track the task of updating the extension documentation. The library docs are currently being worked on in #144. As for checking responses, I would suggest looking at the various result classes in the library. There are getter methods and an
The various other result classes in the library are specific to insert, update, and delete operations. Similarly, they throw if you attempt to access a result field when the result is unacknowledged. |
I'll certainly open new issues in new thread. Thanks for the info. Looks like I'll be upgrading to 2.6+. |
Hi!
I'm using
$collection->updateOne($query, ['$set' => [$prop => $value]]);
While using $set with updateOne, if the value is different than the one currently set for the document, it will return nMatched = 1, nModified = 1. If the value is the same, it will return nMatched = 1 and nModified = 0. I understand why, since it didn't update with the same value. But from a user stand point, I still need to be able to return that the document has been modified to avoid checking if the user is trying to update with the same value (which has no incidence). So question, is there a way to have the driver returning nModified = 1 even if the value was the same?
The text was updated successfully, but these errors were encountered: