@aniya.jaskolski
To update an object in MongoDB using PHP, you can use the updateOne()
or updateMany()
methods. Here is an example of how to update an object:
1 2 3 4 5 |
<?php $mongoClient = new MongoDBClient('mongodb://localhost:27017'); $database = $mongoClient->myDatabase; $collection = $database->myCollection; |
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php $filter = ['_id' => new MongoDBBSONObjectID('5f1c4f68e74a6bb2b1f04e22')]; $update = ['$set' => ['name' => 'John Doe', 'age' => 30]]; $result = $collection->updateOne($filter, $update); if ($result->getModifiedCount() > 0) { echo 'Document updated successfully'; } else { echo 'Document not updated'; } |
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php $filter = ['age' => ['$lt' => 30]]; $update = ['$set' => ['status' => 'inactive']]; $result = $collection->updateMany($filter, $update); if ($result->getModifiedCount() > 0) { echo 'Documents updated successfully'; } else { echo 'Documents not updated'; } |
These are basic examples of how to update objects in MongoDB using PHP. You can modify the filter and update parameters according to your specific requirements.