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:

../../_images/compass-insert-button.png

  • 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 …
  • 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:

  1. { 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
  1. db.inventory.insertOne(
  2. { item: "canvas", qty: 100, tags: ["cotton"], size: { h: 28, w: 35.5, uom: "cm" } }
  3. )

You can run the operation in the web shell below:

../../_images/compass-insert-document-inventory.png

  1. db.inventory.insert_one(
  2. {"item": "canvas",
  3. "qty": 100,
  4. "tags": ["cotton"],
  5. "size": {"h": 28, "w": 35.5, "uom": "cm"}})
  1. Document canvas = new Document("item", "canvas")
  2. .append("qty", 100)
  3. .append("tags", singletonList("cotton"));
  4.  
  5. Document size = new Document("h", 28)
  6. .append("w", 35.5)
  7. .append("uom", "cm");
  8. canvas.put("size", size);
  9.  
  10. collection.insertOne(canvas);
  1. await db.collection('inventory').insertOne({
  2. item: 'canvas',
  3. qty: 100,
  4. tags: ['cotton'],
  5. size: { h: 28, w: 35.5, uom: 'cm' }
  6. });
  1. $insertOneResult = $db->inventory->insertOne([
  2. 'item' => 'canvas',
  3. 'qty' => 100,
  4. 'tags' => ['cotton'],
  5. 'size' => ['h' => 28, 'w' => 35.5, 'uom' => 'cm'],
  6. ]);
  1. await db.inventory.insert_one(
  2. {"item": "canvas",
  3. "qty": 100,
  4. "tags": ["cotton"],
  5. "size": {"h": 28, "w": 35.5, "uom": "cm"}})
  1. Document canvas = new Document("item", "canvas")
  2. .append("qty", 100)
  3. .append("tags", singletonList("cotton"));
  4.  
  5. Document size = new Document("h", 28)
  6. .append("w", 35.5)
  7. .append("uom", "cm");
  8. canvas.put("size", size);
  9.  
  10. Publisher<Success> insertOnePublisher = collection.insertOne(canvas);
  1. var document = new BsonDocument
  2. {
  3. { "item", "canvas" },
  4. { "qty", 100 },
  5. { "tags", new BsonArray { "cotton" } },
  6. { "size", new BsonDocument { { "h", 28 }, { "w", 35.5 }, { "uom", "cm" } } }
  7. };
  8. collection.InsertOne(document);
  1. $db->coll("inventory")->insert_one(
  2. {
  3. item => "canvas",
  4. qty => 100,
  5. tags => ["cotton"],
  6. size => { h => 28, w => 35.5, uom => "cm" }
  7. }
  8. );
  1. client[:inventory].insert_one({ item: 'canvas',
  2. qty: 100,
  3. tags: [ 'cotton' ],
  4. size: { h: 28, w: 35.5, uom: 'cm' } })
  1. collection.insertOne(
  2. Document("item" -> "canvas", "qty" -> 100, "tags" -> Seq("cotton"), "size" -> Document("h" -> 28, "w" -> 35.5, "uom" -> "cm"))
  3. ).execute()
  1. result, err := coll.InsertOne(
  2. context.Background(),
  3. bson.D{
  4. {"item", "canvas"},
  5. {"qty", 100},
  6. {"tags", bson.A{"cotton"}},
  7. {"size", bson.D{
  8. {"h", 28},
  9. {"w", 35.5},
  10. {"uom", "cm"},
  11. }},
  12. })
  • 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.insertedIdpromise contains the _id of the newly inserted document.

Upon successful insert, theinsertOne()method returns an instance ofMongoDB\InsertOneResultwhosegetInsertedId()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
  1. db.inventory.find( { item: "canvas" } )

../../_images/compass-query-collection.png

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.

  1. cursor = db.inventory.find({"item": "canvas"})
  1. FindIterable<Document> findIterable = collection.find(eq("item", "canvas"));
  1. const cursor = db.collection('inventory').find({ item: 'canvas' });
  1. $cursor = $db->inventory->find(['item' => 'canvas']);
  1. cursor = db.inventory.find({"item": "canvas"})
  1. FindPublisher<Document> findPublisher = collection.find(eq("item", "canvas"));
  1. var filter = Builders<BsonDocument>.Filter.Eq("item", "canvas");
  2. var result = collection.Find(filter).ToList();
  1. $cursor = $db->coll("inventory")->find( { item => "canvas" } );
  1. client[:inventory].find(item: 'canvas')
  1. val observable = collection.find(equal("item", "canvas"))
  1. cursor, err := coll.Find(
  2. context.Background(),
  3. bson.D{{"item", "canvas"}},
  4. )

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:

  1. { item: "journal", qty: 25, tags: ["blank", "red"], size: { h: 14, w: 21, uom: "cm" } }
  2. { item: "mat", qty: 85, tags: ["gray"], size: { h: 27.9, w: 35.5, uom: "cm" } }
  3. { 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
  1. db.inventory.insertMany([
  2. { item: "journal", qty: 25, tags: ["blank", "red"], size: { h: 14, w: 21, uom: "cm" } },
  3. { item: "mat", qty: 85, tags: ["gray"], size: { h: 27.9, w: 35.5, uom: "cm" } },
  4. { item: "mousepad", qty: 25, tags: ["gel", "blue"], size: { h: 19, w: 22.85, uom: "cm" } }
  5. ])

You can run the operation in the web shell below:

  1. db.inventory.insert_many([
  2. {"item": "journal",
  3. "qty": 25,
  4. "tags": ["blank", "red"],
  5. "size": {"h": 14, "w": 21, "uom": "cm"}},
  6. {"item": "mat",
  7. "qty": 85,
  8. "tags": ["gray"],
  9. "size": {"h": 27.9, "w": 35.5, "uom": "cm"}},
  10. {"item": "mousepad",
  11. "qty": 25,
  12. "tags": ["gel", "blue"],
  13. "size": {"h": 19, "w": 22.85, "uom": "cm"}}])
  1. Document journal = new Document("item", "journal")
  2. .append("qty", 25)
  3. .append("tags", asList("blank", "red"));
  4.  
  5. Document journalSize = new Document("h", 14)
  6. .append("w", 21)
  7. .append("uom", "cm");
  8. journal.put("size", journalSize);
  9.  
  10. Document mat = new Document("item", "mat")
  11. .append("qty", 85)
  12. .append("tags", singletonList("gray"));
  13.  
  14. Document matSize = new Document("h", 27.9)
  15. .append("w", 35.5)
  16. .append("uom", "cm");
  17. mat.put("size", matSize);
  18.  
  19. Document mousePad = new Document("item", "mousePad")
  20. .append("qty", 25)
  21. .append("tags", asList("gel", "blue"));
  22.  
  23. Document mousePadSize = new Document("h", 19)
  24. .append("w", 22.85)
  25. .append("uom", "cm");
  26. mousePad.put("size", mousePadSize);
  27.  
  28. collection.insertMany(asList(journal, mat, mousePad));
  1. await db.collection('inventory').insertMany([
  2. {
  3. item: 'journal',
  4. qty: 25,
  5. tags: ['blank', 'red'],
  6. size: { h: 14, w: 21, uom: 'cm' }
  7. },
  8. {
  9. item: 'mat',
  10. qty: 85,
  11. tags: ['gray'],
  12. size: { h: 27.9, w: 35.5, uom: 'cm' }
  13. },
  14. {
  15. item: 'mousepad',
  16. qty: 25,
  17. tags: ['gel', 'blue'],
  18. size: { h: 19, w: 22.85, uom: 'cm' }
  19. }
  20. ]);
  1. $insertManyResult = $db->inventory->insertMany([
  2. [
  3. 'item' => 'journal',
  4. 'qty' => 25,
  5. 'tags' => ['blank', 'red'],
  6. 'size' => ['h' => 14, 'w' => 21, 'uom' => 'cm'],
  7. ],
  8. [
  9. 'item' => 'mat',
  10. 'qty' => 85,
  11. 'tags' => ['gray'],
  12. 'size' => ['h' => 27.9, 'w' => 35.5, 'uom' => 'cm'],
  13. ],
  14. [
  15. 'item' => 'mousepad',
  16. 'qty' => 25,
  17. 'tags' => ['gel', 'blue'],
  18. 'size' => ['h' => 19, 'w' => 22.85, 'uom' => 'cm'],
  19. ],
  20. ]);
  1. await db.inventory.insert_many([
  2. {"item": "journal",
  3. "qty": 25,
  4. "tags": ["blank", "red"],
  5. "size": {"h": 14, "w": 21, "uom": "cm"}},
  6. {"item": "mat",
  7. "qty": 85,
  8. "tags": ["gray"],
  9. "size": {"h": 27.9, "w": 35.5, "uom": "cm"}},
  10. {"item": "mousepad",
  11. "qty": 25,
  12. "tags": ["gel", "blue"],
  13. "size": {"h": 19, "w": 22.85, "uom": "cm"}}])
  1. Document journal = new Document("item", "journal")
  2. .append("qty", 25)
  3. .append("tags", asList("blank", "red"));
  4.  
  5. Document journalSize = new Document("h", 14)
  6. .append("w", 21)
  7. .append("uom", "cm");
  8. journal.put("size", journalSize);
  9.  
  10. Document mat = new Document("item", "mat")
  11. .append("qty", 85)
  12. .append("tags", singletonList("gray"));
  13.  
  14. Document matSize = new Document("h", 27.9)
  15. .append("w", 35.5)
  16. .append("uom", "cm");
  17. mat.put("size", matSize);
  18.  
  19. Document mousePad = new Document("item", "mousePad")
  20. .append("qty", 25)
  21. .append("tags", asList("gel", "blue"));
  22.  
  23. Document mousePadSize = new Document("h", 19)
  24. .append("w", 22.85)
  25. .append("uom", "cm");
  26. mousePad.put("size", mousePadSize);
  27.  
  28. Publisher<Success> insertManyPublisher = collection.insertMany(asList(journal, mat, mousePad));
  1. var documents = new BsonDocument[]
  2. {
  3. new BsonDocument
  4. {
  5. { "item", "journal" },
  6. { "qty", 25 },
  7. { "tags", new BsonArray { "blank", "red" } },
  8. { "size", new BsonDocument { { "h", 14 }, { "w", 21 }, { "uom", "cm"} } }
  9. },
  10. new BsonDocument
  11. {
  12. { "item", "mat" },
  13. { "qty", 85 },
  14. { "tags", new BsonArray { "gray" } },
  15. { "size", new BsonDocument { { "h", 27.9 }, { "w", 35.5 }, { "uom", "cm"} } }
  16. },
  17. new BsonDocument
  18. {
  19. { "item", "mousepad" },
  20. { "qty", 25 },
  21. { "tags", new BsonArray { "gel", "blue" } },
  22. { "size", new BsonDocument { { "h", 19 }, { "w", 22.85 }, { "uom", "cm"} } }
  23. },
  24. };
  25. collection.InsertMany(documents);
  1. $db->coll("inventory")->insert_many(
  2. [
  3. {
  4. item => "journal",
  5. qty => 25,
  6. tags => [ "blank", "red" ],
  7. size => { h => 14, w => 21, uom => "cm" }
  8. },
  9. {
  10. item => "mat",
  11. qty => 85,
  12. tags => ["gray"],
  13. size => { h => 27.9, w => 35.5, uom => "cm" }
  14. },
  15. {
  16. item => "mousepad",
  17. qty => 25,
  18. tags => [ "gel", "blue" ],
  19. size => { h => 19, w => 22.85, uom => "cm" }
  20. }
  21. ]
  22. );
  1. client[:inventory].insert_many([{ item: 'journal',
  2. qty: 25,
  3. tags: ['blank', 'red'],
  4. size: { h: 14, w: 21, uom: 'cm' }
  5. },
  6. { item: 'mat',
  7. qty: 85,
  8. tags: ['gray'],
  9. size: { h: 27.9, w: 35.5, uom: 'cm' }
  10. },
  11. { item: 'mousepad',
  12. qty: 25,
  13. tags: ['gel', 'blue'],
  14. size: { h: 19, w: 22.85, uom: 'cm' }
  15. }
  16. ])
  1. collection.insertMany(Seq(
  2. Document("item" -> "journal", "qty" -> 25, "tags" -> Seq("blank", "red"), "size" -> Document("h" -> 14, "w" -> 21, "uom" -> "cm")),
  3. Document("item" -> "mat", "qty" -> 85, "tags" -> Seq("gray"), "size" -> Document("h" -> 27.9, "w" -> 35.5, "uom" -> "cm")),
  4. Document("item" -> "mousepad", "qty" -> 25, "tags" -> Seq("gel", "blue"), "size" -> Document("h" -> 19, "w" -> 22.85, "uom" -> "cm"))
  5. )).execute()
  1. result, err := coll.InsertMany(
  2. context.Background(),
  3. []interface{}{
  4. bson.D{
  5. {"item", "journal"},
  6. {"qty", int32(25)},
  7. {"tags", bson.A{"blank", "red"}},
  8. {"size", bson.D{
  9. {"h", 14},
  10. {"w", 21},
  11. {"uom", "cm"},
  12. }},
  13. },
  14. bson.D{
  15. {"item", "mat"},
  16. {"qty", int32(25)},
  17. {"tags", bson.A{"gray"}},
  18. {"size", bson.D{
  19. {"h", 27.9},
  20. {"w", 35.5},
  21. {"uom", "cm"},
  22. }},
  23. },
  24. bson.D{
  25. {"item", "mousepad"},
  26. {"qty", 25},
  27. {"tags", bson.A{"gel", "blue"}},
  28. {"size", bson.D{
  29. {"h", 19},
  30. {"w", 22.85},
  31. {"uom", "cm"},
  32. }},
  33. },
  34. })
  • 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.InsertManyResultwhose inserted_ids field is a list containing the _idof 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.insertedIdsfield 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\InsertManyResultwhosegetInsertedIds()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.InsertManyResultwhose inserted_ids field is a list containing the _idof 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
  1. db.inventory.find( {} )

../../_images/compass-select-all.png

  1. cursor = db.inventory.find({})
  1. FindIterable<Document> findIterable = collection.find(new Document());
  1. const cursor = db.collection('inventory').find({});
  1. $cursor = $db->inventory->find([]);
  1. cursor = db.inventory.find({})
  1. FindPublisher<Document> findPublisher = collection.find(new Document());
  1. var filter = Builders<BsonDocument>.Filter.Empty;
  2. var result = collection.Find(filter).ToList();
  1. $cursor = $db->coll("inventory")->find( {} );
  1. client[:inventory].find({})
  1. var findObservable = collection.find(Document())
  1. cursor, err := coll.Find(
  2. context.Background(),
  3. bson.D{},
  4. )

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

See also

See also

See also

See also

See also

See also

See also

See also

See also

See also