-
if _id field is omitted when we insert a document, MongoDB driver automatically generates an ObjectId for the_id field. This also applies to documents inserted through update operations.
-
All write operations in MongoDB are atomic on the level of a single document.
Insert method | Description | Multi Document Transaction | Creates Collection On Successful write |
---|---|---|---|
db.collection.insertOne() |
Inserts a single document into a collection. | ☑️ | ☑️ |
db.collection.insertMany() |
Inserts multiple Documents into a collection | ☑️ | ☑️ |
db.collection.insert() |
inserts a single document or multiple documents into a collection. | ☑️ | ☑️ |
db.collection.update() |
when used with usert:true option |
☑️ | |
db.collection.updateOne() |
when used with usert:true option |
☑️ | |
db.collection.updateMany() |
when used with the upsert: true option. |
☑️ | |
db.collection.findAndModify() |
when used with the upsert: true option. |
☑️ | |
db.collection.findOneAndUpdate() |
when used with the upsert:true option. |
☑️ | |
db.collection.findOneAndReplace() |
when used with the upsert:true option. |
☑️ |
db.collection.insertOne(
<document>,
{
writeConcern: <document>
}
)
Parameter | Type | Description |
---|---|---|
document |
document | A document to insert into the collection. |
writeConcern |
document | Optional. A document expressing the write concern. Omit to use the default write concern (i.e. w:1). 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 acknowledged as true if the operation ran with write concern or false if write concern was disabled.
- A field insertedId with the _id value of the inserted document.
⚡ Hands On:
execute the commands below and see what is the response of the operation; make sure you are ontraining_db
db.products.insertOne({item:'Iphone8' , qty:5})
db.products.insertOne({_id:002,item:'fastCharger', qty: 14})
db.products.insertOne({_id:002,item:'Iphone9', qty: 99})
db.products.insertOne({item:"Samsung Galaxy 10", qty:5},{writeConcern:{w:"majority"}})
pause 1 of the secondary replica containers
docker pause mongodb3
and run the command below; you will get an error; what error did you get ?
db.products.insertOne({item:"hwawi M10", qty:5},{writeConcern:{w:3,wtimeout:50}})
db.collection.insertMany(
[ <document 1> , <document 2>, ... ],
{
writeConcern: <document>,
ordered: <boolean>
}
)
Parameter | Type | Description |
---|---|---|
document |
document | An array of documents to insert into the collection. |
writeConcern |
document | Optional. A document expressing the write concern. Omit to use the default write concern (i.e. w:1). |
ordered |
boolean | Optional. A boolean specifying whether the mongod instance should perform an ordered or unordered insert. Defaults to true . Excluding Write Concern errors ordered operations stop after an error, while unordered operations continue to process any remaining write operations in the queue. |
Returns: A document containing:
- A boolean acknowledged as true if the operation ran with write concern or false if write concern was disabled
- An array of _id for each successfully inserted documents
⚡ Hands On:
execute the commands below and see what is the response of the operation; make sure you are ontraining_db
db.alphabets.insertMany([{'letter':'A'},{'letter':'B'}, {'letter':'c'},{'letter':'d'}])
what did you get ?
db.alphabets.deleteMany({})
//clean the collection for the next example
db.alphabets.insertMany([{_id:001,letter:'A'},{_id:001, letter:'B'},{_id:002, letter:'C'}],{ordered:true})
you should get an error bc of duplicate Ids but check which items were inserteddb.alphabets.find({})
db.alphabets.deleteMany({})
//clean the collection for the next example
db.alphabets.insertMany([{_id:001,letter:'A'},{_id:001, letter:'B'},{_id:002, letter:'C'}],{ordered:false})
you should get an error bc of duplicate Ids but check which items were inserteddb.alphabets.find({})
db.collection.insert(
<document or array of documents>,
{
writeConcern: <document>,
ordered: <boolean>
}
)
Parameter | Type | Description |
---|---|---|
document |
document or array | A document or array of documents to insert into the collection. |
writeConcern |
document | Optional. A document expressing the write concern. Omit to use the default write concern. |
ordered |
boolean | Optional. If true , perform an ordered insert of the documents in the array, and if an error occurs with one of documents, MongoDB will return without processing the remaining documents in the array.If false , perform an unordered insert, and if an error occurs with one of documents, continue processing the remaining documents in the array. Defaults to true . |