- MongoDB CRUD Operations >
- MongoDB CRUD Tutorials >
- Insert Documents
Insert Documents¶
On this page
In MongoDB, the db.collection.insert()
method adds new
documents into a collection.
Insert a Document¶
Insert a document into a collection.¶
Insert a document into a collection named inventory
. The
operation will create the collection if the collection does not
currently exist.
The operation returns a WriteResult
object with the status
of the operation. A successful insert of the
document returns the following object:
The nInserted
field specifies the number of
documents inserted. If the operation encounters an error, the
WriteResult
object will contain the error information.
Review the inserted document.¶
If the insert operation is successful, verify the insertion by querying the collection.
The document you inserted should return.
The returned document shows that MongoDB added an _id
field to
the document. If a client inserts a document that does not contain
the _id
field, MongoDB adds the field with the value set to a
generated ObjectId. The
ObjectId values in your documents
will differ from the ones shown.
Insert an Array of Documents¶
You can pass an array of documents to the
db.collection.insert()
method to insert multiple documents.
Create an array of documents.¶
Define a variable mydocuments
that holds an array of documents to
insert.
Insert the documents.¶
Pass the mydocuments
array to the
db.collection.insert()
to perform a bulk insert.
The method returns a BulkWriteResult
object with the
status of the operation. A successful insert of the documents
returns the following object:
The nInserted
field specifies the number of
documents inserted. If the operation encounters an error, the
BulkWriteResult
object will contain information regarding
the error.
The inserted documents will each have an _id
field added by MongoDB.
Insert Multiple Documents with Bulk
¶
New in version 2.6.
MongoDB provides a Bulk()
API that you can use to perform
multiple write operations in bulk. The following sequence of operations
describes how you would use the Bulk()
API to insert a group
of documents into a MongoDB collection.
Initialize a Bulk
operations builder.¶
Initialize a Bulk
operations builder for the collection
inventory
.
The operation returns an unordered operations builder which maintains a list of operations to perform. Unordered operations means that MongoDB can execute in parallel as well as in nondeterministic order. If an error occurs during the processing of one of the write operations, MongoDB will continue to process remaining write operations in the list.
You can also initialize an ordered operations builder; see
db.collection.initializeOrderedBulkOp()
for details.
Add insert operations to the bulk
object.¶
Add two insert operations to the bulk
object using the
Bulk.insert()
method.
Execute the bulk operation.¶
Call the execute()
method on the bulk
object
to execute the operations in its list.
The method returns a BulkWriteResult
object with the
status of the operation. A successful insert of the documents
returns the following object:
The nInserted
field specifies the number of
documents inserted. If the operation encounters an error, the
BulkWriteResult
object will contain information regarding
the error.
Additional Examples and Methods¶
For more examples, see db.collection.insert()
.
The db.collection.update()
method, the
db.collection.findAndModify()
, and the
db.collection.save()
method can also add new documents. See
the individual reference pages for the methods for more information and
examples.