We have covered creation and retrieval of documents in MongoDB. It is time that we learn how we can update a document.
Updates are performed by the update()
method of the MongoCollection
object. This is how the method signature looks:
public bool MongoCollection::update ( $criteria, $newobj, $options )
$criteria
is an array that specifies the document that is going to be updated. The database is queried with $criteria
to select the document intended to be updated.
$newobj
is the document (represented as an array) that is going to replace the old document.
$options
specifies optional arguments to update()
. We will talk about what options are available later.
In this next section, we are going to see this method in action by building a module to edit existing blog posts in the database.