Siva
Siva

Reputation: 1529

Call to undefined method MongoDB::update()

when using the update statement via PHP means am getting the error.

Call to undefined method MongoDB::update()

I'm using the mongo-php-driver driver for connecting the MongoDB and PHP

My Code

$result = $this->mongo_db->update('table_name',array('user_type'=>"D"),array('$set'=>array('account_balance'=>0)),array('upsert'=>true));

My MongoDB reference link is https://docs.mongodb.com/manual/reference/method/db.collection.update/

Upvotes: 3

Views: 3557

Answers (3)

llioor
llioor

Reputation: 6238

You should check if the mongo db extension is installed and available. Install it with pecl:

https://stackoverflow.com/a/74844843/2862728

Upvotes: -1

ash__939
ash__939

Reputation: 1599

From the documentation

Use the MongoDB\Collection::updateOne() method to update a single document matching a filter. MongoDB\Collection::updateOne() returns a MongoDB\UpdateResult object, which you can use to access statistics about the update operation.

<?php

$collection = (new MongoDB\Client)->test->users;
$collection->drop();

$collection->insertOne(['name' => 'Bob', 'state' => 'ny']);
$collection->insertOne(['name' => 'Alice', 'state' => 'ny']);
$updateResult = $collection->updateOne(
    ['state' => 'ny'],
    ['$set' => ['country' => 'us']]
);

printf("Matched %d document(s)\n", $updateResult->getMatchedCount());
printf("Modified %d document(s)\n", $updateResult->getModifiedCount());

For Extra Read:

The MongoDB PHP Library and underlying mongodb extension have notable API differences from the legacy mongo extension. Please read the upgrade guide for more details.

Upvotes: 0

Siva
Siva

Reputation: 1529

Hi am using updateMany instead of update. Its working fine for me.

$result = $this->mongo_db->updateMany('table_name',array('user_type'=>"D"),array('$set'=>array('account_balance'=>0)),array('upsert'=>true));

Upvotes: 2

Related Questions