- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
MongoDB - Java
- MongoDB - Java Setup
- MongoDB - Java - Create Collection
- MongoDB - Java - Get Collection
- MongoDB - Java - Insert Document
- MongoDB - Java - Retrieve Documents
- MongoDB - Java - Update Document
- MongoDB - Java - Delete Document
- MongoDB - Java - Drop Collection
- MongoDB - Java - List Collections
MongoDB - PHP
- MongoDB - PHP Setup
- MongoDB - PHP - Create Collection
- MongoDB - PHP - Get Collection
- MongoDB - PHP - Insert Document
- MongoDB - PHP - Retrieve Documents
- MongoDB - PHP - Update Document
- MongoDB - PHP - Delete Document
- MongoDB - PHP - Drop Collection
- MongoDB - PHP - List Collections
MongoDB - Advanced
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
MongoDB - Useful Resources
MongoDB - PHP - Updating a Document
MongoDB provides MongoCollection class to update a document in a collection.
Syntax
// select a collection
$collection = $db->mycol;
// now update the document
$collection->updateOne(array("title"=>"MongoDB"),
array('$set'=>array("title"=>"MongoDB Tutorial")));
echo "<br/>Document updated successfully.<br/>";
// now display the updated document
$cursor = $collection->find();
// iterate cursor to display title of documents
echo "Updated document";
foreach ($cursor as $document) {
echo $document["title"] . "<br/>";
}
Updating Document in a Collection
To update a document, we first need to connect to a database and then select the collection and then select and update the document as shown below −
<?php
require __DIR__ . '\vendor\autoload.php';
try {
$uri = "mongodb://localhost:27017";
// connect to mongodb
$client = new MongoDB\Client($uri);
echo "Connection to database successful.";
// select a database
$db = $client->myDb;
echo "<br/>Database myDb selected.";
$collection = $db->mycol;
echo "<br/>Collection selected succsessfully.";
// now update the document
$collection->updateOne(array("title"=>"MongoDB"),
array('$set'=>array("title"=>"MongoDB Tutorial")));
echo "<br/>Document updated successfully.<br/>";
// now display the updated document
$cursor = $collection->find();
// iterate cursor to display title of documents
echo "Updated document. <br/>";
foreach ($cursor as $document) {
echo $document["title"] . "<br/>";
}
} catch (MongoDB\Driver\Exception\Exception $e) {
echo "Exception:", $e->getMessage(), "\n";
}
?>
Output
Now, let's compile and run the above program to update a document in a collection in our database myDb.
On executing, the above program gives you the following output.
Connection to database successful. Database myDb selected. Collection selected succsessfully. Document updated successfully. Updated document. MongoDB Tutorial
Advertisements