- Reference >
mongoshMethods >- Collection Methods >
- db.collection.insertOne()
db.collection.insertOne()¶
On this page
Definition¶
-
db.collection.insertOne()¶ Important
mongoshMethodThis page documents a
mongoshmethod. This is not the documentation for a language-specific driver such as Node.js.For MongoDB API drivers, refer to the language-specific :driver:`MongoDB driver documentation </>`.
For the legacy
mongoshell documentation, refer to the documentation for the corresponding MongoDB Server release:Inserts a single document into a collection.
The
insertOne()method has the following syntax:Parameter Type Description documentdocument A document to insert into the collection. writeConcerndocument Optional. A document expressing the write concern. Omit to use the default write concern.
Do not explicitly set the write concern for the operation if run in a transaction. To use write concern with transactions, see Transactions and Write Concern.
Returns: A document containing: - A boolean
acknowledgedastrueif the operation ran with write concern orfalseif write concern was disabled. - A field
insertedIdwith the_idvalue of the inserted document.
- A boolean
Behaviors¶
Collection Creation¶
If the collection does not exist, then the
insertOne() method creates the collection.
_id Field¶
If the document does not specify an _id field, then mongod
will add the _id field and assign a unique
ObjectId for the document before inserting. Most
drivers create an ObjectId and insert the _id field, but the
mongod will create and populate the _id if the driver or
application does not.
If the document contains an _id field, the _id value must be
unique within the collection to avoid duplicate key error.
Explainability¶
insertOne() is not compatible with
db.collection.explain().
Error Handling¶
On error, db.collection.insertOne() throws either a writeError
or writeConcernError exception.
Transactions¶
db.collection.insertOne() can be used inside multi-document transactions.
Important
In most cases, multi-document transaction incurs a greater performance cost over single document writes, and the availability of multi-document transactions should not be a replacement for effective schema design. For many scenarios, the denormalized data model (embedded documents and arrays) will continue to be optimal for your data and use cases. That is, for many scenarios, modeling your data appropriately will minimize the need for multi-document transactions.
For additional transactions usage considerations (such as runtime limit and oplog size limit), see also Production Considerations.
Collection Creation in Transactions¶
Starting in MongoDB 4.4, you can create collections and indexes inside a multi-document transaction if the transaction is :red:`not` a cross-shard write transaction.
Specifically, in MongoDB 4.4 and greater, if you specify an insert on a non-existing collection in a transaction, the collection is implicitly created.
In MongoDB 4.4 and earlier, the operation must be run on an existing collection.
Write Concerns and Transactions¶
Do not explicitly set the write concern for the operation if run in a transaction. To use write concern with transactions, see Transactions and Write Concern.
Examples¶
Insert a Document without Specifying an _id Field¶
In the following example, the document passed to the
insertOne() method does not contain the _id
field:
The operation returns the following document:
Because the documents did not include _id,
mongod creates and adds the _id field and
assigns it a unique ObjectId value.
The ObjectId values are specific to the machine and time when the
operation is run. As such, your values may differ from those in the
example.
Insert a Document Specifying an _id Field¶
In the following example, the document passed to the
insertOne() method includes the _id field.
The value of _id must be unique within the collection to avoid
duplicate key error.
The operation returns the following:
Inserting an duplicate value for any key that is part of a unique
index, such as _id, throws an exception. The following attempts to insert
a document with a _id value that already exists:
Since _id: 10 already exists, the following exception is thrown:
Increase Write Concern¶
Given a three member replica set, the following operation specifies a
w of majority, wtimeout of 100:
If the acknowledgement takes longer than the wtimeout limit, the following
exception is thrown:
See also
- To insert multiple documents, see
db.collection.insertMany() WriteResult.writeConcernError