Docs Menu
Docs Home
/ / /
PHP Library Manual

Write Data to MongoDB

On this page

  • Overview
  • Sample Application
  • Insert One
  • Insert Multiple
  • Update One
  • Update Multiple
  • Replace One
  • Delete One
  • Delete Multiple
  • Bulk Write
  • Store Large Files

On this page, you can see copyable code examples that show common MongoDB PHP Library methods for writing data to MongoDB.

Tip

To learn more about any of the methods shown on this page, see the link provided in each section.

To use an example from this page, copy the code example into the sample application or your own application. Make sure to set the MONGODB_URI environment variable to the connection string for your MongoDB deployment, and replace the <database> and <collection> placeholders with values for your target namespace.

You can use the following sample application to test the code examples on this page. To use the sample application, perform the following steps:

  1. Ensure you have the MongoDB PHP Library installed in your project. To learn more about installing the MongoDB PHP Library, see the Download and Install guide.

  2. Copy the following code and paste it into a new .php file.

  3. Copy a code example from this page and paste it on the specified lines in the file.

1<?php
2
3require __DIR__ . '/../vendor/autoload.php';
4
5$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset');
6$client = new MongoDB\Client($uri);
7
8$collection = $client->selectCollection('<database>', '<collection>');
9
10// Start example code here
11
12// End example code here

The following code shows how to insert a single document into a collection:

$result = $collection->insertOne([
'<field name 1>' => '<value 1>',
'<field name 2>' => '<value 2>',
]);

To learn more about the MongoDB\Collection::insertOne() method, see the Insert Documents guide.

The following code shows how to insert multiple documents into a collection:

$result = $collection->insertMany(
[
'<field name 1>' => '<value 1>',
'<field name 2>' => '<value 2>',
],
[
'<field name 1>' => '<value 1>',
'<field name 2>' => '<value 2>',
],
);

To learn more about the MongoDB\Collection::insertMany() method, see the Insert Documents guide.

The following code shows how to update a single document in a collection by creating or editing a field:

$result = $collection->updateOne(
['<field to match>' => '<value to match>'],
['$set' => ['<field name>' => '<value>']],
);

To learn more about the MongoDB\Collection::updateOne() method, see the Update Documents guide.

The following code shows how to update multiple documents in a collection by creating or editing a field:

$result = $collection->updateMany(
['<field to match>' => '<value to match>'],
['$set' => ['<field name>' => '<value>']],
);

To learn more about the MongoDB\Collection::updateMany() method, see the Update Documents guide.

The following code shows how to replace a single document in a collection with another document:

$result = $collection->replaceOne(
['<field to match>' => '<value to match>'],
[
'<new field 1>' => '<value 1>',
'<new field 2>' => '<value 2>',
],
);

To learn more about the MongoDB\Collection::replaceOne() method, see the Replace Documents guide.

The following code shows how to delete a single document in a collection:

$result = $collection->deleteOne(['<field name>' => '<value>']);

To learn more about the MongoDB\Collection::deleteOne() method, see the Delete Documents guide.

The following code shows how to delete multiple documents in a collection:

$result = $collection->deleteMany(['<field name>' => '<value>']);

To learn more about the MongoDB\Collection::deleteMany() method, see the Delete Documents guide.

The following code shows how to perform multiple write operations in a single bulk operation:

$result = $collection->bulkWrite(
[
[
'insertOne' => [
['<field name>' => '<value>'],
],
],
[
'replaceOne' => [
['<field to match>' => '<value to match>'],
[
'<first new field>' => '<value>',
'<second new field>' => '<value>',
],
],
],
[
'updateOne' => [
['<field to match>' => '<value to match>'],
['$set' => ['<field to update>' => '<value to update>']],
],
],
[
'updateMany' => [
['<field to match>' => '<value to match>'],
['$set' => ['<field to update>' => '<value to update>']],
],
],
[
'deleteOne' => [
['<field name>' => '<value>'],
],
],
[
'deleteMany' => [
['<field name>' => '<value>'],
],
],
]
);

To learn more about the MongoDB\Collection::bulkWrite() method, see the Bulk Write guide.

The following code shows how to store files in a GridFS bucket by creating an upload stream:

$bucket = $client->selectDatabase('<database name>')->selectGridFSBucket();
$stream = $bucket->openUploadStream('<file name>');
fwrite($stream, '<data>');
fclose($stream);

To learn more about GridFS, see the Store Large Files guide.

Back

Monitor Changes