Navigation
This version of the documentation is archived and no longer supported.

Write Operations Overview

A write operation is any operation that creates or modifies data in the MongoDB instance. In MongoDB, write operations target a single collection. All write operations in MongoDB are atomic on the level of a single document.

There are three classes of write operations in MongoDB: insert, update, and remove. Insert operations add new data to a collection. Update operations modify existing data, and remove operations delete data from a collection. No insert, update, or remove can affect more than one document atomically.

For the update and remove operations, you can specify criteria, or conditions, that identify the documents to update or remove. These operations use the same query syntax to specify the criteria as read operations.

After issuing these modification operations, MongoDB allows applications to determine the level of acknowledgment returned from the database. See Write Concern.

Create

Create operations add new documents to a collection. In MongoDB, the db.collection.insert() method performs create operations.

The following diagram highlights the components of a MongoDB insert operation:

The components of a MongoDB insert operations.

The following diagram shows the same query in SQL:

The components of a SQL INSERT statement.

Example

The following operation inserts a new documents into the users collection. The new document has four fields name, age, and status, and an _id field. MongoDB always adds the _id field to the new document if that field does not exist.

db.users.insert(
   {
      name: "sue",
      age: 26,
      status: "A"
   }
)

This operation inserts a new document into the users collection. The new document has four fields: name, age, status, and an _id field. MongoDB always adds the _id field to a new document if the field does not exist.

For more information, see db.collection.insert() and Insert Documents.

Some updates also create records. If an update operation specifies the upsert flag and there are no documents that match the query portion of the update operation, then MongoDB will convert the update into an insert.

With an upsert, applications can decide between performing an update or an insert operation using just a single call. Both the update() method and the save() method can perform an upsert. See update() and save() for details on performing an upsert with these methods.

See

SQL to MongoDB Mapping Chart for additional examples of MongoDB write operations and the corresponding SQL statements.

Insert Behavior

If you add a new document without the _id field, the client library or the mongod instance adds an _id field and populates the field with a unique ObjectId.

If you specify the _id field, the value must be unique within the collection. For operations with write concern, if you try to create a document with a duplicate _id value, mongod returns a duplicate key exception.

Update

Update operations modify existing documents in a collection. In MongoDB, db.collection.update() and the db.collection.save() methods perform update operations. The db.collection.update() method can accept query criteria to determine which documents to update as well as an option to update multiple rows. The method can also accept options that affect its behavior such as the multi option to update multiple documents.

The following diagram highlights the components of a MongoDB update operation:

The components of a MongoDB update operation.

The following diagram shows the same query in SQL:

The components of a SQL UPDATE statement.

Example

db.users.update(
   { age: { $gt: 18 } },
   { $set: { status: "A" } },
   { multi: true }
)

This update operation on the users collection sets the status field to A for the documents that match the criteria of age greater than 18.

For more information, see db.collection.update() and db.collection.save(), and Modify Documents for examples.

Update Behavior

By default, the db.collection.update() method updates a single document. However, with the multi option, update() can update all documents in a collection that match a query.

The db.collection.update() method either updates specific fields in the existing document or replaces the document. See db.collection.update() for details.

When performing update operations that increase the document size beyond the allocated space for that document, the update operation relocates the document on disk and may reorder the document fields depending on the type of update.

The db.collection.save() method replaces a document and can only update a single document. See db.collection.save() and Insert Documents for more information

Delete

Delete operations remove documents from a collection. In MongoDB, db.collection.remove() method performs delete operations. The db.collection.remove() method can accept query criteria to determine which documents to remove.

The following diagram highlights the components of a MongoDB remove operation:

The components of a MongoDB remove operation.

The following diagram shows the same query in SQL:

The components of a SQL DELETE statement.

Example

db.users.remove(
   { status: "D" }
)

This delete operation on the users collection removes all documents that match the criteria of status equal to D.

For more information, see db.collection.remove() method and Remove Documents.

Remove Behavior

By default, db.collection.remove() method removes all documents that match its query. However, the method can accept a flag to limit the delete operation to a single document.

Isolation of Write Operations

The modification of a single document is always atomic, even if the write operation modifies multiple sub-documents within that document. For write operations that modify multiple documents, the operation as a whole is not atomic, and other operations may interleave.

No other operations are atomic. You can, however, attempt to isolate a write operation that affects multiple documents using the isolation operator.

To isolate a sequence of write operations from other read and write operations, see Perform Two Phase Commits.