Comments (7)
There are three possibilities:
- nMatched = 0, nModified = 0: the criteria matched nothing
- nMatched = 1, nModified = 0: the criteria matched but the
$set
resulted in no change to the document - nMatched = 1, nModified = 1: the criteria matched and
$set
changed the document
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.
If I understand correctly, you don't care if $set
changes the document at all. It seems like your error condition is the criteria not matching anything, in which case you can just use nMatched.
This only occurs if the end user updates a property with the same value.
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 $set
into the update criteria with $ne
to ensure that you never find a document that already has the value you're attempting to change. This wouldn't work with other updates (e.g. $inc
) but it seems quite feasible for $set
, where you know exactly what the post-modified value would become.
from mongo-php-library.
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 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)
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 nModified
indeed reports. You then say that there is no incidence where you should be updating the same value, which is the case that would cause nMatched = 1 and nModified = 0.
from mongo-php-library.
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.
from mongo-php-library.
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.
By no incidence I meant that it doesn't matter to me that the end user updates with the same value, it shouldn't be considered as a "fault" but still considered as an update. Sorry for not explaining properly.
from mongo-php-library.
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:
$filter = [
'_id' => new \MongoDB\BSON\ObjectID('56e345f426d0f93939d833cb')
];
$data = [
'$set' => [
'hello_world' => 'testing3'
]
];
$response = $collection->updateOne($filter, $data);
Updated document:
{
"_id" : ObjectId("56e345f426d0f93939d833cb"),
"email" : "[email protected]",
"first_name" : "Testing",
"hello_world" : "testing3"
}
Environment
Windows 10
Wamp
PHP 5.5.12
Mongodb driver 1.1.4
Latest version of new php library (Installed with composer: composer require "mongodb/mongodb=^1.0.0")
Mongo instance version 2.4.6
from mongo-php-library.
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.
My updateOne() code below does in fact update the mongo document, but nModified is always returned as "null". How can I verify a document has been updated?
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 UpdateResult::getModifiedCount()
in the library or WriteResult::getModifiedCount()
in the extension.
The other issue is, with legacy mongo driver on an update I could simply look for $response['ok']. However, when checking the response with $response['isAcknowledged'] I'm getting: "Fatal error: Cannot use object of type MongoDB\UpdateResult as array". How do I check responses?
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 isAcknowledged()
method for relevant write result fields. If you're using the extension directly, you will want to refer to the WriteResult methods. Note that since the extension only provides an executeBulkWrite()
API for issuing writes, its WriteResult is akin to the library's BulkWriteResult class. The library just adds exceptions when accessing results if the write is unacknowledged in order to comply with the CRUD specification:
If you have a choice, consider providing the acknowledged member and raising an error if the other fields are accessed in an unacknowledged write. Instead of users receiving a null reference exception, you have the opportunity to provide an informative error message indicating the correct way to handle the situation.
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.
from mongo-php-library.
I'll certainly open new issues in new thread. Thanks for the info. Looks like I'll be upgrading to 2.6+.
from mongo-php-library.
Related Issues (20)
- PHP+MongoDB: "Return value must be of type MongoDB\Driver\Server, null returned" HOT 3
- Can we drop `jean85/pretty-package-versions:1.2`? HOT 4
- Decision about `mongo-orchestration` folder and `Makefile` HOT 3
- [Documentation] Syntax highlighting in the method reference is not ideal HOT 1
- A never-ending query after upgrade to MongoDB 6.0.3 HOT 6
- Memory leak on updateOne in the loop HOT 1
- Deprecated: Return type of MongoDB\Model\BSONDocument::bsonSerialize() HOT 2
- The StreamWrapper class is currently unusable without the bucket HOT 4
- Performance issue with PHP 8.2 HOT 2
- Extract mongodb-1.6.2.tgz error HOT 1
- MacOs M1 Cannot connect to Atlas After upgrade driver HOT 7
- This is a test
- $unset not properly work on multiple field? HOT 1
- Docs: missing `]` character in database->aggregate documentation HOT 1
- Question about fieldPaths (feature request?) HOT 1
- Non blocking io HOT 1
- $where is not allowed in this context HOT 2
- TLS connection with mongo cluster failed (while single host works) HOT 2
- getting result from mongodb with toArray() HOT 3
- Getting the error in driver version 1.14 HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mongo-php-library.