insert() and relevant
In MongoDB, collections are used to store documents. To add data into these collections, MongoDB provides two primary insertion methods: insertOne()
and insertMany()
. In this section, we’ll explore the usage and syntax of these methods, along with their options and some basic examples.
insertOne()
The insertOne()
method is used to insert a single document into a collection. This method returns an InsertOneResult
object, that shows the outcome of the operation.
Syntax:
db.collection.insertOne(
<document>,
{
writeConcern: <document>,
ordered: <boolean>,
bypassDocumentValidation: <boolean>,
comment: <any>
}
)
Options:
writeConcern:
An optional document specifying the level of acknowledgment requested from MongoDB for the write operation.ordered:
An optional boolean flag. When set totrue
, MongoDB will return an error if it encounters a duplicate document in the operation. Default is alsotrue
.bypassDocumentValidation:
Optional boolean flag. To validate or not to validate the document against the collection’s validation rules. Default isfalse
.comment:
An optional string or BSON that can be used for descriptive purposes when profiling operations.
Example:
db.inventory.insertOne({
item: 'book',
qty: 1,
});
insertMany()
The insertMany()
method is used to insert multiple documents into a collection at once. It returns an InsertManyResult
object, displaying the status of the operation.
Syntax:
db.collection.insertMany(
[ <document_1>, <document_2>, ... ],
{
writeConcern: <document>,
ordered: <boolean>,
bypassDocumentValidation: <boolean>,
comment: <any>
}
)
Options:
writeConcern:
Same as mentioned ininsertOne()
method.ordered:
Same as mentioned ininsertOne()
method. When set totrue
, MongoDB will insert the documents in the array’s order. If a fail occurs, it will stop further processing of the documents. Default istrue
.bypassDocumentValidation:
Same as mentioned ininsertOne()
method.comment:
Same as mentioned ininsertOne()
method.
Example:
db.inventory.insertMany([
{ item: 'pen', qty: 5 },
{ item: 'pencil', qty: 10 },
{ item: 'notebook', qty: 25 },
]);
In conclusion, insert methods in MongoDB allow users to add documents to a collection with a few simple commands. By understanding the syntax and options available for insertOne()
and insertMany()
, we can efficiently store and manage data within MongoDB collections.