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 willcreate the collection.
Insert a Single Document
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
- PHP
- Motor
- Java (Async)
- C#
- Perl
- Ruby
- Scala
- Go
db.collection.insertOne()
inserts a singledocument into a collection.
The following example inserts a new document into theinventory
collection. If the document does not specifyan _id
field, MongoDB adds the _id
field with anObjectId value to the new document. SeeInsert Behavior.
To insert a single document using MongoDB Compass:
Navigate to the collection you wish to insert the documentinto:
- In the left-hand MongoDB Compass navigation pane, clickthe 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 andfill in the field name and value. Add fields by clickingthe last line number, then clickingAdd Field After …
- For
Object
types, add nested fields by clicking thelast field’s number and selectingAdd Field After … - For
Array
types, add additional elements to the arrayby clicking the last element’s line number and selectingAdd Array Element After …
- For
- Once all fields have been filled out, click Insert.
The following example inserts a new document into thetest.inventory
collection:
pymongo.collection.Collection.insert_one()
inserts asingledocument into acollection.
The following example inserts a new document into theinventory
collection. If the document does not specifyan _id
field, the PyMongo driver adds the _id
fieldwith an ObjectId value to the new document. SeeInsert Behavior.
com.mongodb.client.MongoCollection.insertOneinserts a singledocument intoa collection.
The following example inserts a new document into theinventory
collection. If the document does not specifyan _id
field, the driver adds the _id
field with anObjectId value to the new document. SeeInsert Behavior.
Collection.insertOne()inserts a singledocument into acollection.
The following example inserts a new document into theinventory
collection. If the document does not specifyan _id
field, the Node.js driver adds the _id
fieldwith an ObjectId value to the new document. SeeInsert Behavior.
MongoDB\Collection::insertOne()
inserts a singledocument into acollection.
The following example inserts a new document into theinventory
collection. If the document does not specifyan _id
field, the PHP driver adds the _id
fieldwith an ObjectId value to the new document. SeeInsert Behavior.
motor.motor_asyncio.AsyncIOMotorCollection.insert_one()
inserts asingledocument into acollection.
The following example inserts a new document into theinventory
collection. If the document does not specifyan _id
field, the Motor driver adds the _id
fieldwith an ObjectId value to the new document. SeeInsert Behavior.
com.mongodb.reactivestreams.client.MongoCollection.insertOne)inserts a singledocument intoa collection with the Java Reactive StreamsDriver:
- { item: "canvas", qty: 100, tags: ["cotton"], size: { h: 28, w: 35.5, uom: "cm" } }
The following example inserts the document above into theinventory
collection. If the document does not specifyan _id
field, the driver adds the _id
field with anObjectId value to the new document. SeeInsert Behavior.
IMongoCollection.InsertOne()inserts a singledocument into acollection.
The following example inserts a new document into theinventory
collection. If the document does not specifyan _id
field, the C# driver adds the _id
fieldwith an ObjectId value to the new document. SeeInsert Behavior.
MongoDB::Collection::insert_one()inserts a singledocument into acollection.
The following example inserts a new document into theinventory
collection. If the document does not specifyan _id
field, the Perl driver adds the _id
fieldwith an ObjectId value to the new document. SeeInsert Behavior.
Mongo::Collection#insert_one()inserts a singledocument into acollection.
The following example inserts a new document into theinventory
collection. If the document does not specifyan _id
field, the Ruby driver adds the _id
fieldwith an ObjectId value to the new document. SeeInsert Behavior.
collection.insertOne():org.mongodb.scala.SingleObservable[org.mongodb.scala.Completed])inserts a singledocument into acollection.
The following example inserts a new document into theinventory
collection. If the document does not specifyan _id
field, the Scala driver adds the _id
fieldwith an ObjectId value to the new document. SeeInsert Behavior.
Collection.InsertOneinserts a singledocument into acollection.
The following example inserts a new document into theinventory
collection. If the document does not specifyan _id
field, the driver adds the _id
fieldwith an ObjectId value to the new document. SeeInsert Behavior.
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
- PHP
- Motor
- Java (Async)
- C#
- Perl
- Ruby
- Scala
- Go
- db.inventory.insertOne(
- { item: "canvas", qty: 100, tags: ["cotton"], size: { h: 28, w: 35.5, uom: "cm" } }
- )
You can run the operation in the web shell below:
- db.inventory.insert_one(
- {"item": "canvas",
- "qty": 100,
- "tags": ["cotton"],
- "size": {"h": 28, "w": 35.5, "uom": "cm"}})
- Document canvas = new Document("item", "canvas")
- .append("qty", 100)
- .append("tags", singletonList("cotton"));
- Document size = new Document("h", 28)
- .append("w", 35.5)
- .append("uom", "cm");
- canvas.put("size", size);
- collection.insertOne(canvas);
- await db.collection('inventory').insertOne({
- item: 'canvas',
- qty: 100,
- tags: ['cotton'],
- size: { h: 28, w: 35.5, uom: 'cm' }
- });
- $insertOneResult = $db->inventory->insertOne([
- 'item' => 'canvas',
- 'qty' => 100,
- 'tags' => ['cotton'],
- 'size' => ['h' => 28, 'w' => 35.5, 'uom' => 'cm'],
- ]);
- await db.inventory.insert_one(
- {"item": "canvas",
- "qty": 100,
- "tags": ["cotton"],
- "size": {"h": 28, "w": 35.5, "uom": "cm"}})
- Document canvas = new Document("item", "canvas")
- .append("qty", 100)
- .append("tags", singletonList("cotton"));
- Document size = new Document("h", 28)
- .append("w", 35.5)
- .append("uom", "cm");
- canvas.put("size", size);
- Publisher<Success> insertOnePublisher = collection.insertOne(canvas);
- var document = new BsonDocument
- {
- { "item", "canvas" },
- { "qty", 100 },
- { "tags", new BsonArray { "cotton" } },
- { "size", new BsonDocument { { "h", 28 }, { "w", 35.5 }, { "uom", "cm" } } }
- };
- collection.InsertOne(document);
- $db->coll("inventory")->insert_one(
- {
- item => "canvas",
- qty => 100,
- tags => ["cotton"],
- size => { h => 28, w => 35.5, uom => "cm" }
- }
- );
- client[:inventory].insert_one({ item: 'canvas',
- qty: 100,
- tags: [ 'cotton' ],
- size: { h: 28, w: 35.5, uom: 'cm' } })
- collection.insertOne(
- Document("item" -> "canvas", "qty" -> 100, "tags" -> Seq("cotton"), "size" -> Document("h" -> 28, "w" -> 35.5, "uom" -> "cm"))
- ).execute()
- result, err := coll.InsertOne(
- context.Background(),
- bson.D{
- {"item", "canvas"},
- {"qty", 100},
- {"tags", bson.A{"cotton"}},
- {"size", bson.D{
- {"h", 28},
- {"w", 35.5},
- {"uom", "cm"},
- }},
- })
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
- PHP
- Motor
- Java (Async)
- C#
- Perl
- Ruby
- Scala
- Go
insertOne()
returns a document thatincludes the newly inserted document’s _id
field value. Foran example of a return document, seedb.collection.insertOne() reference.
Note
MongoDB Compass generates the _id
field and its valueautomatically. The generatedObjectId consists of aunique randomly generated hexadecimal value.
You can change this value prior to inserting your documentso long as it remains unique and is a valid ObjectId
.For more information on the _id
field, see_id Field.
insert_one()
returns aninstance of pymongo.results.InsertOneResult
whoseinserted_id
field contains the _id
of the newlyinserted document.
insertOne() returns apromise that provides a result
. The result.insertedId
promise contains the _id
of the newly inserted document.
Upon successful insert, theinsertOne()
method returns an instance ofMongoDB\InsertOneResult
whosegetInsertedId()
method returns the _id
of the newly inserted document.
insert_one()
returns aninstance of pymongo.results.InsertOneResult
whoseinserted_id
field contains the _id
of the newlyinserted document.
com.mongodb.reactivestreams.client.MongoCollection.insertOne)returns a Publisherobject. The Publisher
inserts the document into a collection when subscribers request data.
Upon successful insert, theinsert_one() method returnsan instance ofMongoDB::InsertOneResult whoseinserted_id
attribute contains the _id
of the newlyinserted document.
Upon successful insert, theinsert_one()method returns an instance ofMongo::Operation::Result, whoseinserted_id
attribute contains the _id
of the newlyinserted document.
Upon successful insert, thecollection.insertOne():org.mongodb.scala.SingleObservable[org.mongodb.scala.Completed])method returns an instance ofcollection.insertOne().results(); whoseinserted_id
attribute contains the _id
of the newlyinserted document.
Collection.InsertOnefunction returns an instance ofInsertOneResult whoseInsertedID
attribute contains the _id
of the newlyinserted document.
To retrieve the document that you just inserted, query the collection:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
- PHP
- Motor
- Java (Async)
- C#
- Perl
- Ruby
- Scala
- Go
- db.inventory.find( { item: "canvas" } )
Specify a filter in the MongoDB Compass query bar and clickFind to execute the query.
The above filter specifies that MongoDB Compass only returndocuments where the item
field is equal to canvas
.
For more information on the MongoDB Compass Query Bar, see theCompassQuery Bardocumentation.
- cursor = db.inventory.find({"item": "canvas"})
- FindIterable<Document> findIterable = collection.find(eq("item", "canvas"));
- const cursor = db.collection('inventory').find({ item: 'canvas' });
- $cursor = $db->inventory->find(['item' => 'canvas']);
- cursor = db.inventory.find({"item": "canvas"})
- FindPublisher<Document> findPublisher = collection.find(eq("item", "canvas"));
- var filter = Builders<BsonDocument>.Filter.Eq("item", "canvas");
- var result = collection.Find(filter).ToList();
- $cursor = $db->coll("inventory")->find( { item => "canvas" } );
- client[:inventory].find(item: 'canvas')
- val observable = collection.find(equal("item", "canvas"))
- cursor, err := coll.Find(
- context.Background(),
- bson.D{{"item", "canvas"}},
- )
Insert Multiple Documents
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
- PHP
- Motor
- Java (Async)
- C#
- Perl
- Ruby
- Scala
- Go
New in version 3.2.
db.collection.insertMany()
can insert multipledocuments into a collection. Pass anarray of documents to the method.
The following example inserts three new documents into theinventory
collection. If the documents do not specify an_id
field, MongoDB adds the _id
field with an ObjectIdvalue to each document. See Insert Behavior.
Currently, MongoDB Compass does not support inserting multipledocuments in a single operation.
New in version 3.2.
pymongo.collection.Collection.insert_many()
can insertmultipledocuments into acollection. Pass an iterable of documents to the method.
The following example inserts three new documents into theinventory
collection. If the documents do not specify an_id
field, the PyMongo driver adds the _id
field withan ObjectId value to each document. See Insert Behavior.
New in version 3.2.
com.mongodb.client.MongoCollection.insertManycan insert multipledocumentsinto a collection. Pass a list of documents to the method.
The following example inserts three new documents into theinventory
collection. If the documents do not specify an_id
field, the driver adds the _id
field withan ObjectId value to each document. See Insert Behavior.
New in version 3.2.
Collection.insertMany()can insert multipledocumentsinto a collection. Pass an array of documents to the method.
The following example inserts three new documents into theinventory
collection. If the documents do not specify an_id
field, the Node.js driver adds the _id
field withan ObjectId value to each document. SeeInsert Behavior.
New in version 3.2.
MongoDB\Collection::insertMany()
can insert multipledocuments into acollection. Pass an array of documents to the method.
The following example inserts three new documents into theinventory
collection. If the documents do not specify an_id
field, the PHP driver adds the _id
field withan ObjectId value to each document. See Insert Behavior.
motor.motor_asyncio.AsyncIOMotorCollection.insert_many()
can insert multipledocumentsinto a collection. Pass an iterable of documents to the method.
The following example inserts three new documents into theinventory
collection. If the documents do not specify an_id
field, the PyMongo driver adds the _id
field withan 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 StreamsDriver:
- { item: "journal", qty: 25, tags: ["blank", "red"], size: { h: 14, w: 21, uom: "cm" } }
- { item: "mat", qty: 85, tags: ["gray"], size: { h: 27.9, w: 35.5, uom: "cm" } }
- { item: "mousepad", qty: 25, tags: ["gel", "blue"], size: { h: 19, w: 22.85, uom: "cm" } }
The following example inserts three new documents into theinventory
collection. If the documents do not specify an_id
field, the driver adds the _id
field withan ObjectId value to each document. See Insert Behavior.
New in version 3.2.
IMongoCollection.InsertMany()can insert multipledocumentsinto a collection. Pass an enumerable collection of documentsto the method.
The following example inserts three new documents into theinventory
collection. If the documents do not specify an_id
field, the driver adds the _id
field withan ObjectId value to each document. See Insert Behavior.
New in version 3.2.
MongoDB::Collection::insert_many()can insert multipledocuments into acollection. Pass an array reference of documents to the method.
The following example inserts three new documents into theinventory
collection. If the documents do not specify an_id
field, the Perl driver adds the _id
field withan ObjectId value to each document. See Insert Behavior.
New in version 3.2.
Mongo::Collection#insert_many()can insert multipledocuments into acollection. Pass an array of documents to the method.
The following example inserts three new documents into theinventory
collection. If the documents do not specify an_id
field, the Ruby driver adds the _id
field withan ObjectId value to each document. See Insert Behavior.
New in version 3.2.
collection.insertMany():org.mongodb.scala.SingleObservable[org.mongodb.scala.Completed])can insert multipledocuments into acollection.
The following example inserts three new documents into theinventory
collection. If the documents do not specify an_id
field, the Scala driver adds the _id
field withan ObjectId value to each document. See Insert Behavior.
Collection.InsertManycan insert multipledocuments into acollection.
The following example inserts three new documents into theinventory
collection. If the documents do not specify an_id
field, the driver adds the _id
field withan ObjectId value to each document. See Insert Behavior.
- Mongo Shell
- Python
- Java (Sync)
- Node.js
- PHP
- Other
- Motor
- Java (Async)
- C#
- Perl
- Ruby
- Scala
- Go
- db.inventory.insertMany([
- { item: "journal", qty: 25, tags: ["blank", "red"], size: { h: 14, w: 21, uom: "cm" } },
- { item: "mat", qty: 85, tags: ["gray"], size: { h: 27.9, w: 35.5, uom: "cm" } },
- { item: "mousepad", qty: 25, tags: ["gel", "blue"], size: { h: 19, w: 22.85, uom: "cm" } }
- ])
You can run the operation in the web shell below:
- db.inventory.insert_many([
- {"item": "journal",
- "qty": 25,
- "tags": ["blank", "red"],
- "size": {"h": 14, "w": 21, "uom": "cm"}},
- {"item": "mat",
- "qty": 85,
- "tags": ["gray"],
- "size": {"h": 27.9, "w": 35.5, "uom": "cm"}},
- {"item": "mousepad",
- "qty": 25,
- "tags": ["gel", "blue"],
- "size": {"h": 19, "w": 22.85, "uom": "cm"}}])
- Document journal = new Document("item", "journal")
- .append("qty", 25)
- .append("tags", asList("blank", "red"));
- Document journalSize = new Document("h", 14)
- .append("w", 21)
- .append("uom", "cm");
- journal.put("size", journalSize);
- Document mat = new Document("item", "mat")
- .append("qty", 85)
- .append("tags", singletonList("gray"));
- Document matSize = new Document("h", 27.9)
- .append("w", 35.5)
- .append("uom", "cm");
- mat.put("size", matSize);
- Document mousePad = new Document("item", "mousePad")
- .append("qty", 25)
- .append("tags", asList("gel", "blue"));
- Document mousePadSize = new Document("h", 19)
- .append("w", 22.85)
- .append("uom", "cm");
- mousePad.put("size", mousePadSize);
- collection.insertMany(asList(journal, mat, mousePad));
- await db.collection('inventory').insertMany([
- {
- item: 'journal',
- qty: 25,
- tags: ['blank', 'red'],
- size: { h: 14, w: 21, uom: 'cm' }
- },
- {
- item: 'mat',
- qty: 85,
- tags: ['gray'],
- size: { h: 27.9, w: 35.5, uom: 'cm' }
- },
- {
- item: 'mousepad',
- qty: 25,
- tags: ['gel', 'blue'],
- size: { h: 19, w: 22.85, uom: 'cm' }
- }
- ]);
- $insertManyResult = $db->inventory->insertMany([
- [
- 'item' => 'journal',
- 'qty' => 25,
- 'tags' => ['blank', 'red'],
- 'size' => ['h' => 14, 'w' => 21, 'uom' => 'cm'],
- ],
- [
- 'item' => 'mat',
- 'qty' => 85,
- 'tags' => ['gray'],
- 'size' => ['h' => 27.9, 'w' => 35.5, 'uom' => 'cm'],
- ],
- [
- 'item' => 'mousepad',
- 'qty' => 25,
- 'tags' => ['gel', 'blue'],
- 'size' => ['h' => 19, 'w' => 22.85, 'uom' => 'cm'],
- ],
- ]);
- await db.inventory.insert_many([
- {"item": "journal",
- "qty": 25,
- "tags": ["blank", "red"],
- "size": {"h": 14, "w": 21, "uom": "cm"}},
- {"item": "mat",
- "qty": 85,
- "tags": ["gray"],
- "size": {"h": 27.9, "w": 35.5, "uom": "cm"}},
- {"item": "mousepad",
- "qty": 25,
- "tags": ["gel", "blue"],
- "size": {"h": 19, "w": 22.85, "uom": "cm"}}])
- Document journal = new Document("item", "journal")
- .append("qty", 25)
- .append("tags", asList("blank", "red"));
- Document journalSize = new Document("h", 14)
- .append("w", 21)
- .append("uom", "cm");
- journal.put("size", journalSize);
- Document mat = new Document("item", "mat")
- .append("qty", 85)
- .append("tags", singletonList("gray"));
- Document matSize = new Document("h", 27.9)
- .append("w", 35.5)
- .append("uom", "cm");
- mat.put("size", matSize);
- Document mousePad = new Document("item", "mousePad")
- .append("qty", 25)
- .append("tags", asList("gel", "blue"));
- Document mousePadSize = new Document("h", 19)
- .append("w", 22.85)
- .append("uom", "cm");
- mousePad.put("size", mousePadSize);
- Publisher<Success> insertManyPublisher = collection.insertMany(asList(journal, mat, mousePad));
- var documents = new BsonDocument[]
- {
- new BsonDocument
- {
- { "item", "journal" },
- { "qty", 25 },
- { "tags", new BsonArray { "blank", "red" } },
- { "size", new BsonDocument { { "h", 14 }, { "w", 21 }, { "uom", "cm"} } }
- },
- new BsonDocument
- {
- { "item", "mat" },
- { "qty", 85 },
- { "tags", new BsonArray { "gray" } },
- { "size", new BsonDocument { { "h", 27.9 }, { "w", 35.5 }, { "uom", "cm"} } }
- },
- new BsonDocument
- {
- { "item", "mousepad" },
- { "qty", 25 },
- { "tags", new BsonArray { "gel", "blue" } },
- { "size", new BsonDocument { { "h", 19 }, { "w", 22.85 }, { "uom", "cm"} } }
- },
- };
- collection.InsertMany(documents);
- $db->coll("inventory")->insert_many(
- [
- {
- item => "journal",
- qty => 25,
- tags => [ "blank", "red" ],
- size => { h => 14, w => 21, uom => "cm" }
- },
- {
- item => "mat",
- qty => 85,
- tags => ["gray"],
- size => { h => 27.9, w => 35.5, uom => "cm" }
- },
- {
- item => "mousepad",
- qty => 25,
- tags => [ "gel", "blue" ],
- size => { h => 19, w => 22.85, uom => "cm" }
- }
- ]
- );
- client[:inventory].insert_many([{ item: 'journal',
- qty: 25,
- tags: ['blank', 'red'],
- size: { h: 14, w: 21, uom: 'cm' }
- },
- { item: 'mat',
- qty: 85,
- tags: ['gray'],
- size: { h: 27.9, w: 35.5, uom: 'cm' }
- },
- { item: 'mousepad',
- qty: 25,
- tags: ['gel', 'blue'],
- size: { h: 19, w: 22.85, uom: 'cm' }
- }
- ])
- collection.insertMany(Seq(
- Document("item" -> "journal", "qty" -> 25, "tags" -> Seq("blank", "red"), "size" -> Document("h" -> 14, "w" -> 21, "uom" -> "cm")),
- Document("item" -> "mat", "qty" -> 85, "tags" -> Seq("gray"), "size" -> Document("h" -> 27.9, "w" -> 35.5, "uom" -> "cm")),
- Document("item" -> "mousepad", "qty" -> 25, "tags" -> Seq("gel", "blue"), "size" -> Document("h" -> 19, "w" -> 22.85, "uom" -> "cm"))
- )).execute()
- result, err := coll.InsertMany(
- context.Background(),
- []interface{}{
- bson.D{
- {"item", "journal"},
- {"qty", int32(25)},
- {"tags", bson.A{"blank", "red"}},
- {"size", bson.D{
- {"h", 14},
- {"w", 21},
- {"uom", "cm"},
- }},
- },
- bson.D{
- {"item", "mat"},
- {"qty", int32(25)},
- {"tags", bson.A{"gray"}},
- {"size", bson.D{
- {"h", 27.9},
- {"w", 35.5},
- {"uom", "cm"},
- }},
- },
- bson.D{
- {"item", "mousepad"},
- {"qty", 25},
- {"tags", bson.A{"gel", "blue"}},
- {"size", bson.D{
- {"h", 19},
- {"w", 22.85},
- {"uom", "cm"},
- }},
- },
- })
- Mongo Shell
- Python
- Java (Sync)
- Node.js
- PHP
- Other
- Motor
- Java (Async)
- C#
- Perl
- Ruby
- Scala
- Go
insertMany()
returns a document that includesthe newly inserted documents _id
field values. See thereference for an example.
To retrieve the inserted documents, query the collection:
insert_many()
returnsan 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 apromise that provides a result
. The result.insertedIds
field contains an array with the _id
of each newly inserteddocument.
To retrieve the inserted documents, query the collection:
Upon successful insert, theinsertMany()
methodreturns an instance ofMongoDB\InsertManyResult
whosegetInsertedIds()
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 Publisherobject. The Publisher
inserts the document into a collectionwhen subscribers request data.
To retrieve the inserted documents, query the collection:
To retrieve the inserted documents, query the collection:
Upon successful insert, theinsert_many() methodreturns an instance ofMongoDB::InsertManyResultwhose 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, theinsert_many() methodreturns an instance ofMongo::BulkWrite::Resultwhose 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, theinsertMany():org.mongodb.scala.SingleObservable[org.mongodb.scala.Completed])method returns an Observable with a type parameter indicating whenthe operation has completed or with either acom.mongodb.DuplicateKeyException
orcom.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
- PHP
- Motor
- Java (Async)
- C#
- Perl
- Ruby
- Scala
- Go
- db.inventory.find( {} )
- cursor = db.inventory.find({})
- FindIterable<Document> findIterable = collection.find(new Document());
- const cursor = db.collection('inventory').find({});
- $cursor = $db->inventory->find([]);
- cursor = db.inventory.find({})
- FindPublisher<Document> findPublisher = collection.find(new Document());
- var filter = Builders<BsonDocument>.Filter.Empty;
- var result = collection.Find(filter).ToList();
- $cursor = $db->coll("inventory")->find( {} );
- client[:inventory].find({})
- var findObservable = collection.find(Document())
- cursor, err := coll.Find(
- context.Background(),
- bson.D{},
- )
Insert Behavior
Collection Creation
If the collection does not currently exist, insert operations willcreate 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 inserteddocument omits the _id
field, the MongoDB driver automaticallygenerates an ObjectId for the _id
field.
This also applies to documents inserted through updateoperations with upsert: true.
Atomicity
All write operations in MongoDB are atomic on the level of a singledocument. For more information on MongoDB and atomicity, seeAtomicity and Transactions
Write Acknowledgement
With write concerns, you can specify the level of acknowledgementrequested from MongoDB for write operations. For details, seeWrite Concern.
- Mongo Shell
- Python
- Java (Sync)
- Node.js
- PHP
- Other
- Motor
- Java (Async)
- C#
- Perl
- Ruby
- Scala
- Go
See also
See also
pymongo.collection.Collection.insert_one()
pymongo.collection.Collection.insert_many()
- Additional Methods for Inserts
See also
- com.mongodb.client.MongoCollection.insertOne
- com.mongodb.client.MongoCollection.insertMany
- Additional Java Synchronous Driver Write Examples
See also
See also
See also
motor.motor_asyncio.AsyncIOMotorCollection.insert_one()
motor.motor_asyncio.AsyncIOMotorCollection.insert_many()
- Additional Methods for Inserts
See also
- com.mongodb.reactivestreams.client.MongoCollection.insertOne)
- com.mongodb.reactivestreams.client.MongoCollection.html.insertMany)
- Java Reactive Streams Driver Quick Tour
See also
See also
See also
See also
- collection.insertOne():org.mongodb.scala.SingleObservable[org.mongodb.scala.Completed])
- collection.insertMany():org.mongodb.scala.SingleObservable[org.mongodb.scala.Completed])
- Additional Methods for Inserts
See also