1

My output in PHP from a var_dump() shows the result of a updateOne MongoDB command to a collection:

object(MongoDB\UpdateResult)#136 (2) {  
    ["writeResult":"MongoDB\UpdateResult":private] => object(MongoDB\Driver\WriteResult)#135 (9) { 
        ["nInserted"]=> int(0) 
        ["nMatched"]=> int(0) 
        ["nModified"]=> int(0) 
        "nRemoved"]=> int(0) 
        ["nUpserted"]=> int(0) 
        ["upsertedIds"]=> array(0) { } 
        ["writeErrors"]=> array(0) { } 
        ["writeConcernError"]=> NULL 
        ["writeConcern"]=> array(4) { 
            ["w"]=> NULL 
            ["wmajority"]=> bool(false) 
            ["wtimeout"]=> int(0) 
            ["journal"]=> NULL 
        } 
    } 
    ["isAcknowledged":"MongoDB\UpdateResult":private]=> bool(true) 
} 

My question is, how do I convert this into PHP and access the value for "nMatched" and "nInserted"?

1
  • According to mongodb.github.io/mongo-php-library/api/… there's a method called getMatchedCount to get the nMatched value, however there does not seem to be an equivalent one for nInserted, which probably makes sense since this is an UpdateResult and would only have modified values. Commented Oct 19, 2016 at 13:59

1 Answer 1

2

You can use the returned object directly. For accessing matched you can do:

$result->getMatchedCount();

adn for the inserted:

$result->getUpsertedCount();

This is the right and easiest way. You can use this as a reference http://mongodb.github.io/mongo-php-library/api/class-MongoDB.UpdateResult.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.