- MongoDB CRUD Operations >
- Insert Documents
Insert Documents¶
This page provides examples in:
This page provides examples of insert operations in MongoDB.
Creating a Collection
If the collection does not currently exist, insert operations will create the collection.
Insert a Single Document¶
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
db.collection.insertOne()
inserts a single
document into a collection.
The following example inserts a new document into the
inventory
collection. If the document does not specify
an _id
field, MongoDB adds the _id
field with an
ObjectId value to the new document. See
Insert Behavior.
To insert a single document using MongoDB Compass:
Navigate to the collection you wish to insert the document into:
- In the left-hand MongoDB Compass navigation pane, click the database to which your target collection belongs.
- From the database view, click the target collection name.
Click the Insert Document button:
For each field in the document, select the field type and fill in the field name and value. Add fields by clicking the last line number, then clicking Add Field After …
- For
Object
types, add nested fields by clicking the last field’s number and selecting Add Field After … - For
Array
types, add additional elements to the array by clicking the last element’s line number and selecting Add Array Element After …
- For
Once all fields have been filled out, click Insert.
The following example inserts a new document into the
test.inventory
collection:
pymongo.collection.Collection.insert_one()
inserts a
single document into a
collection.
The following example inserts a new document into the
inventory
collection. If the document does not specify
an _id
field, the PyMongo driver adds the _id
field
with an ObjectId value to the new document. See
Insert Behavior.
com.mongodb.client.MongoCollection.insertOne inserts a single document into a collection.
The following example inserts a new document into the
inventory
collection. If the document does not specify
an _id
field, the driver adds the _id
field with an
ObjectId value to the new document. See
Insert Behavior.
Collection.insertOne() inserts a single document into a collection.
The following example inserts a new document into the
inventory
collection. If the document does not specify
an _id
field, the Node.js driver adds the _id
field
with an ObjectId value to the new document. See
Insert Behavior.
MongoDB\Collection::insertOne()
inserts a single document into a
collection.
The following example inserts a new document into the
inventory
collection. If the document does not specify
an _id
field, the PHP driver adds the _id
field
with an ObjectId value to the new document. See
Insert Behavior.
motor.motor_asyncio.AsyncIOMotorCollection.insert_one()
inserts a
single document into a
collection.
The following example inserts a new document into the
inventory
collection. If the document does not specify
an _id
field, the Motor driver adds the _id
field
with an ObjectId value to the new document. See
Insert Behavior.
com.mongodb.reactivestreams.client.MongoCollection.insertOne inserts a single document into a collection with the Java Reactive Streams Driver:
The following example inserts the document above into the
inventory
collection. If the document does not specify
an _id
field, the driver adds the _id
field with an
ObjectId value to the new document. See
Insert Behavior.
IMongoCollection.InsertOne() inserts a single document into a collection.
The following example inserts a new document into the
inventory
collection. If the document does not specify
an _id
field, the C# driver adds the _id
field
with an ObjectId value to the new document. See
Insert Behavior.
MongoDB::Collection::insert_one() inserts a single document into a collection.
The following example inserts a new document into the
inventory
collection. If the document does not specify
an _id
field, the Perl driver adds the _id
field
with an ObjectId value to the new document. See
Insert Behavior.
Mongo::Collection#insert_one() inserts a single document into a collection.
The following example inserts a new document into the
inventory
collection. If the document does not specify
an _id
field, the Ruby driver adds the _id
field
with an ObjectId value to the new document. See
Insert Behavior.
collection.insertOne() inserts a single document into a collection.
The following example inserts a new document into the
inventory
collection. If the document does not specify
an _id
field, the Scala driver adds the _id
field
with an ObjectId value to the new document. See
Insert Behavior.
Collection.InsertOne inserts a single document into a collection.
The following example inserts a new document into the
inventory
collection. If the document does not specify
an _id
field, the driver adds the _id
field
with an ObjectId value to the new document. See
Insert Behavior.
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
You can run the operation in the web shell below:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
insertOne()
returns a document that
includes the newly inserted document’s _id
field value. For
an example of a return document, see
db.collection.insertOne() reference.
Note
MongoDB Compass generates the _id
field and its value
automatically. The generated
ObjectId consists of a
unique randomly generated hexadecimal value.
You can change this value prior to inserting your document
so long as it remains unique and is a valid ObjectId
.
For more information on the _id
field, see
_id Field.
insert_one()
returns an
instance of pymongo.results.InsertOneResult
whose
inserted_id
field contains the _id
of the newly
inserted document.
insertOne() returns a
promise that provides a result
. The result.insertedId
promise contains the _id
of the newly inserted document.
Upon successful insert, the
insertOne()
method returns an instance of
MongoDB\InsertOneResult
whose
getInsertedId()
method returns the _id
of the newly inserted document.
insert_one()
returns an
instance of pymongo.results.InsertOneResult
whose
inserted_id
field contains the _id
of the newly
inserted document.
com.mongodb.reactivestreams.client.MongoCollection.insertOne
returns a Publisher
object. The Publisher
inserts the document into a collection when subscribers request data.
Upon successful insert, the
insert_one() method returns
an instance of
MongoDB::InsertOneResult whose
inserted_id
attribute contains the _id
of the newly
inserted document.
Upon successful insert, the
insert_one()
method returns an instance of
Mongo::Operation::Result, whose
inserted_id
attribute contains the _id
of the newly
inserted document.
Upon successful insert, the
collection.insertOne()
method returns an instance of
collection.insertOne().results(); whose
inserted_id
attribute contains the _id
of the newly
inserted document.
Collection.InsertOne
function returns an instance of
InsertOneResult whose
InsertedID
attribute contains the _id
of the newly
inserted document.
To retrieve the document that you just inserted, query the collection:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
Specify a filter in the MongoDB Compass query bar and click Find to execute the query.
The above filter specifies that MongoDB Compass only return
documents where the item
field is equal to canvas
.
For more information on the MongoDB Compass Query Bar, see the Compass Query Bar documentation.
Insert Multiple Documents¶
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
New in version 3.2.
db.collection.insertMany()
can insert multiple
documents into a collection. Pass an
array of documents to the method.
The following example inserts three new documents into the
inventory
collection. If the documents do not specify an
_id
field, MongoDB adds the _id
field with an ObjectId
value to each document. See Insert Behavior.
Currently, MongoDB Compass does not support inserting multiple documents in a single operation.
New in version 3.2.
pymongo.collection.Collection.insert_many()
can insert
multiple documents into a
collection. Pass an iterable of documents to the method.
The following example inserts three new documents into the
inventory
collection. If the documents do not specify an
_id
field, the PyMongo driver adds the _id
field with
an ObjectId value to each document. See Insert Behavior.
New in version 3.2.
com.mongodb.client.MongoCollection.insertMany can insert multiple documents into a collection. Pass a list of documents to the method.
The following example inserts three new documents into the
inventory
collection. If the documents do not specify an
_id
field, the driver adds the _id
field with
an ObjectId value to each document. See Insert Behavior.
New in version 3.2.
Collection.insertMany() can insert multiple documents into a collection. Pass an array of documents to the method.
The following example inserts three new documents into the
inventory
collection. If the documents do not specify an
_id
field, the Node.js driver adds the _id
field with
an ObjectId value to each document. See
Insert Behavior.
New in version 3.2.
MongoDB\Collection::insertMany()
can insert multiple documents into a
collection. Pass an array of documents to the method.
The following example inserts three new documents into the
inventory
collection. If the documents do not specify an
_id
field, the PHP driver adds the _id
field with
an ObjectId value to each document. See Insert Behavior.
motor.motor_asyncio.AsyncIOMotorCollection.insert_many()
can insert multiple documents
into a collection. Pass an iterable of documents to the method.
The following example inserts three new documents into the
inventory
collection. If the documents do not specify an
_id
field, the PyMongo driver adds the _id
field with
an ObjectId value to each document. See Insert Behavior.
New in version 3.2.
com.mongodb.reactivestreams.client.MongoCollection.html.insertMany inserts the following documents with the Java Reactive Streams Driver:
The following example inserts three new documents into the
inventory
collection. If the documents do not specify an
_id
field, the driver adds the _id
field with
an ObjectId value to each document. See Insert Behavior.
New in version 3.2.
IMongoCollection.InsertMany() can insert multiple documents into a collection. Pass an enumerable collection of documents to the method.
The following example inserts three new documents into the
inventory
collection. If the documents do not specify an
_id
field, the driver adds the _id
field with
an ObjectId value to each document. See Insert Behavior.
New in version 3.2.
MongoDB::Collection::insert_many() can insert multiple documents into a collection. Pass an array reference of documents to the method.
The following example inserts three new documents into the
inventory
collection. If the documents do not specify an
_id
field, the Perl driver adds the _id
field with
an ObjectId value to each document. See Insert Behavior.
New in version 3.2.
Mongo::Collection#insert_many() can insert multiple documents into a collection. Pass an array of documents to the method.
The following example inserts three new documents into the
inventory
collection. If the documents do not specify an
_id
field, the Ruby driver adds the _id
field with
an ObjectId value to each document. See Insert Behavior.
New in version 3.2.
collection.insertMany() can insert multiple documents into a collection.
The following example inserts three new documents into the
inventory
collection. If the documents do not specify an
_id
field, the Scala driver adds the _id
field with
an ObjectId value to each document. See Insert Behavior.
Collection.InsertMany can insert multiple documents into a collection.
The following example inserts three new documents into the
inventory
collection. If the documents do not specify an
_id
field, the driver adds the _id
field with
an ObjectId value to each document. See Insert Behavior.
- Mongo Shell
- Python
- Java (Sync)
- Node.js
- PHP
- Other
You can run the operation in the web shell below:
- Mongo Shell
- Python
- Java (Sync)
- Node.js
- PHP
- Other
insertMany()
returns a document that includes
the newly inserted documents _id
field values. See the
reference for an example.
To retrieve the inserted documents, query the collection:
insert_many()
returns
an instance of pymongo.results.InsertManyResult
whose inserted_ids
field is a list containing the _id
of each newly inserted document.
To retrieve the inserted documents, query the collection:
To retrieve the inserted documents, query the collection:
insertMany() returns a
promise that provides a result
. The result.insertedIds
field contains an array with the _id
of each newly inserted
document.
To retrieve the inserted documents, query the collection:
Upon successful insert, the
insertMany()
method
returns an instance of
MongoDB\InsertManyResult
whose
getInsertedIds()
method returns the _id
of each newly inserted document.
To retrieve the inserted documents, query the collection:
insert_many()
returns an instance of pymongo.results.InsertManyResult
whose inserted_ids
field is a list containing the _id
of each newly inserted document.
To retrieve the inserted documents, query the collection:
com.mongodb.reactivestreams.client.MongoCollection.html.insertMany
returns a Publisher
object. The Publisher
inserts the document into a collection
when subscribers request data.
To retrieve the inserted documents, query the collection:
To retrieve the inserted documents, query the collection:
Upon successful insert, the
insert_many() method
returns an instance of
MongoDB::InsertManyResult
whose inserted_ids
attribute is a list containing the
_id
of each newly inserted document.
To retrieve the inserted documents, query the collection:
Upon successful insert, the
insert_many() method
returns an instance of
Mongo::BulkWrite::Result
whose inserted_ids
attribute is a list containing the
_id
of each newly inserted document.
To retrieve the inserted documents, query the collection:
Upon successful insert, the
insertMany()
method returns an Observable with a type parameter indicating when
the operation has completed or with either a
com.mongodb.DuplicateKeyException
or
com.mongodb.MongoException
.
To retrieve the inserted documents, query the collection:
To retrieve the inserted documents, query the collection:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
Insert Behavior¶
Collection Creation¶
If the collection does not currently exist, insert operations will create the collection.
_id
Field¶
In MongoDB, each document stored in a collection requires a unique
_id field that acts as a primary key. If an inserted
document omits the _id
field, the MongoDB driver automatically
generates an ObjectId for the _id
field.
This also applies to documents inserted through update operations with upsert: true.
Atomicity¶
All write operations in MongoDB are atomic on the level of a single document. For more information on MongoDB and atomicity, see Atomicity and Transactions
Write Acknowledgement¶
With write concerns, you can specify the level of acknowledgement requested from MongoDB for write operations. For details, see Write Concern.
- Mongo Shell
- Python
- Java (Sync)
- Node.js
- PHP
- Other
See also
motor.motor_asyncio.AsyncIOMotorCollection.insert_one()
motor.motor_asyncio.AsyncIOMotorCollection.insert_many()
- Additional Methods for Inserts