Docs Menu
Docs Home
/ / /
Laravel MongoDB
/

Write Operations

On this page

  • Overview
  • Insert One
  • Insert Multiple
  • Update One
  • Update Multiple
  • Upsert
  • Delete One
  • Delete Multiple

In this guide, you can see code templates of common methods that you can use to write data to MongoDB by using Laravel MongoDB.

Tip

To learn more about any of the methods included in this guide, see the links provided in each section.

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

SampleModel::create([
'<field name>' => '<value>',
'<field name>' => '<value>',
...
]);

To view a runnable example that inserts one document, see the Insert a Document usage example.

To learn more about inserting documents, see the Insert Documents guide.

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

SampleModel::insert([
[
'<field name>' => '<value>',
'<field name>' => '<value>',
],
[
'<field name>' => '<value>',
'<field name>' => '<value>',
],
...
]);

To view a runnable example that inserts multiple documents, see the Insert Multiple Documents usage example.

To learn more about inserting documents, see the Insert Documents guide.

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

SampleModel::where('<field name>', '<value>')
->orderBy('<field to sort on>')
->first()
->update([
'<field to update>' => '<new value>',
]);

To view a runnable example that updates one document, see the Update a Document usage example.

To learn more about updating documents, see the Modify Documents guide.

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

SampleModel::where('<field name>', '<comparison operator>', '<value>')
->update(['<field to update>' => '<new value>']);

To view a runnable example that updates multiple documents, see the Update Multiple Documents usage example.

To learn more about updating documents, see the Modify Documents guide.

The following code shows how to update a document, or insert one if a matching document doesn't exist:

SampleModel::where(['<field name>' => '<value>'])
->update(
['<field to update>' => '<new value>', ...],
['upsert' => true],
);
/* Or, use the upsert() method. */
SampleModel::upsert(
[<documents to update or insert>],
'<unique field name>',
[<fields to update>],
);

To learn more about upserting documents, see the Modify Documents guide.

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

SampleModel::where('<field name>', '<value>')
->orderBy('<field to sort on>')
->limit(1)
->delete();

To view a runnable example that deletes one document, see the Delete a Document usage example.

To learn more about deleting documents, see the Delete Documents guide.

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

SampleModel::where('<field name>', '<value>')
->delete();

To view a runnable example that deletes multiple documents, see the Delete Multiple Documents usage example.

To learn more about deleting documents, see the Delete Documents guide.

Back

Read Operations