Reputation: 1529
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
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
Reputation: 1599
Use the
MongoDB\Collection::updateOne()
method to update a single document matching a filter.MongoDB\Collection::updateOne()
returns aMongoDB\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());
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
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