bulkWrite in MongoDB – Detailed 2021 Guide

BulkWrite In MongoDB Featured Image

Here I am back with yet another MongoDB tutorial on – bulkWrite in MongoDB.

MongoDB is a well-known record-situated data set administration framework that permits designers to store mammoth measures of information. It is open-source and exceptionally effective. In spite of the fact that it can in some cases get very perplexing for amateurs when learning without an ODM, it has exact documentation. It is a NoSQL type information base that is it stores information in BSON design as reports inside assortments.

Top-level like Facebook, Google, Adobe, Nokia, and numerous others have picked MongoDB to store their enormous measures of information.

MongoDB offers an assortment of operators, capacities, and techniques that can be utilized for an assortment of purposes. It has given designers straightforward just as adaptable administrators that permit them to do significantly more than CRUD in a MongoDB-upheld application. This is likewise one justification for why MongoDB is so well known.

With the assistance of operators and functions in MongoDB, you redo questioning, printing, editing, and the sky is the limit from there. It has discovered being a fan even among novices alongside senior developers.

bulkWrite in MongoDB simply helps us to perform bulk writing operations in the order of execution with controls. You can always choose to go for an unordered operation by setting the ordered option as false.

The bulkWrite in MongoDB Syntax

In this guide, I will explain with examples bulkWrite in MongoDB in detail with different operations. So, let’s get started.

db.collection.bulkWrite(
   [ operation 1, operation 2, ... ],
   {
      writeConcern : document,
      ordered : boolean
   }
)

bulkWrite in MongoDB accepts an array of operations to it. Let us proceed with the examples with different operations in bulkWrite in MongoDB.

The bulkWrite operation can be used only with a specific number of write operations. These are:

  • insertOne
  • updateOne
  • updateMany
  • deleteOne
  • deleteMany
  • replaceOne

Using bulkWrite in MongoDB with insertOne

  • Start up the Mongo server and open the Mongo shell
  • Choose the database you want to use and navigate into it
  • Let us take a look at the currently available documents in our collection:
> db.shoes.find({}).pretty()
{
        "_id" : ObjectId("616714c6461c6d31d88b4d90"),
        "name" : "Dmitri Vu's Highs",
        "price" : 5300,
        "size" : 8,
        "style" : "Chelsea Boots",
        "__v" : 0,
        "onSale" : false,
        "freeDelivery" : false
}
{
        "_id" : ObjectId("616714c6461c6d31d88b4d91"),
        "name" : "Purple Haven Lavender Kittens",
        "price" : 14610,
        "size" : 9,
        "style" : "Kitten Heels",
        "onSale" : false,
        "freeDelivery" : false
}
{
        "_id" : ObjectId("616714c6461c6d31d88b4d92"),
        "name" : "Marlo Wood’s Lite Moccasins",
        "price" : 8880,
        "size" : 9,
        "style" : "Moccasins",
        "onSale" : false,
        "freeDelivery" : false
}
{
        "_id" : ObjectId("616714c6461c6d31d88b4d93"),
        "name" : "Myrna's D'Orsays",
        "price" : 9760,
        "size" : 7,
        "style" : "D'Orsay",
        "__v" : 0,
        "onSale" : false,
        "freeDelivery" : false
}
{
        "_id" : ObjectId("616716ad33e9f410d4cdfded"),
        "name" : "Choco-Lita YMX 60",
        "price" : 3400,
        "size" : 9,
        "style" : "Lita",
        "__v" : 0,
        "onSale" : false,
        "freeDelivery" : false
}
{
        "_id" : ObjectId("61671f63e97d0a62702d57b5"),
        "name" : "Starvis Thigh Highs Mango Yellow",
        "price" : 10580,
        "size" : 8,
        "style" : "Thigh High Boots",
        "onSale" : false,
        "freeDelivery" : false
}
{
        "_id" : ObjectId("61685f0884b1142f7b3ec59d"),
        "name" : "Pink Belly Shoes",
        "price" : 2580,
        "size" : 7,
        "style" : "Belly Shoes",
        "onSale" : true,
        "freeDelivery" : true
}
{
        "_id" : ObjectId("6169b42739fcb911f638b069"),
        "name" : "Athena Silver Kitten Heels",
        "price" : 13380,
        "size" : 6,
        "style" : "Kitten Heels",
        "onSale" : true,
        "freeDelivery" : true
}
{
        "_id" : ObjectId("6169b42739fcb911f638b06a"),
        "name" : "Brenda’s Desert Wedges",
        "price" : 18580,
        "size" : 8,
        "style" : "Wedges",
        "onSale" : false,
        "freeDelivery" : true
}
  • Now, we will perform the bulkWrite in MongoDB operation with the insertOne operation in our sample collection:
> db.shoes.bulkWrite([ { insertOne: { document: { "name" : "Golden Shimmery Pumps",
...         "price" : 17930,
...         "size" : 7,
...         "style" : "Pumps",
...         "onSale" : true,
...         "freeDelivery" : false
... } } }])


{
        "acknowledged" : true,
        "deletedCount" : 0,
        "insertedCount" : 1,
        "matchedCount" : 0,
        "upsertedCount" : 0,
        "insertedIds" : {
                "0" : ObjectId("6169c15039fcb911f638b06b")
        },
        "upsertedIds" : {

        }
}

Using bulkWrite in MongoDB with updateOne

The updateOne operation with bulkWrite in MongoDB will only update the first matching document to the filter.

> db.shoes.bulkWrite([
... {
...      updateOne:
...            {
...                 "filter": { "name" : "Marlo Wood’s Lite Moccasins" },
...                 "update" : { $set : { "name" : "Marlo Wood’s Teal Moccasins" } }
...             }
... }
... ])


{
        "acknowledged" : true,
        "deletedCount" : 0,
        "insertedCount" : 0,
        "matchedCount" : 1,
        "upsertedCount" : 0,
        "insertedIds" : {

        },
        "upsertedIds" : {

        }
}

Using bulkWrite in MongoDB with deleteOne

We will now use the deleteOne operation with bulkWrite in MongoDB to delete a document.

> db.shoes.bulkWrite([
... { deleteOne :
...             { "filter" : { "name" : "Myrna's D'Orsays" } }
...          },
... ])


{
        "acknowledged" : true,
        "deletedCount" : 1,
        "insertedCount" : 0,
        "matchedCount" : 0,
        "upsertedCount" : 0,
        "insertedIds" : {

        },
        "upsertedIds" : {

        }
}

Using bulkWrite in MongoDB with replaceOne

We will now use the replaceOne operation with bulkWrite in MongoDB to replace a value of a document.

> db.shoes.bulkWrite([
...          { replaceOne :
...             {
...                "filter" : { "name" : "Brenda’s Desert Wedges" },
...                "replacement" : { "name" : "Brenda’s Desert Wedges (Limited Edition)", "onSale" : true, "price" : 13900 }
...             }
...          }
...  ])


{
        "acknowledged" : true,
        "deletedCount" : 0,
        "insertedCount" : 0,
        "matchedCount" : 1,
        "upsertedCount" : 0,
        "insertedIds" : {

        },
        "upsertedIds" : {

        }
}

Performing bulkWrite in MongoDB Using All Operations Together

Let’s now work on performing a bulkWrite in MongoDB using the operations together. We’ll see an example to understand this method now.

> db.shoes.bulkWrite(
...       [
...          { insertOne :
...             {
...                "document" :
...                {
...                   "name" : "Miranda Bane’s Ankle Boots", "size" : 6, "style" : "Ankle Boots", "price" : 30000, "onSale" : true, "freeDelivery" : false
...                }
...             }
...          },
...          { updateOne :
...             {
...                "filter" : { "name" : "Starvis Thigh Highs Mango Yellow" },
...                "update" : { $set : { "price" : 12350 } }
...             }
...          },
...          { deleteOne :
...             { "filter" : { "name" : "Purple Haven Lavender Kittens" } }
...          },
...          { replaceOne :
...             {
...                "filter" : { "name" : "Choco-Lita YMX 60" },
...                "replacement" : { "freeDelivery" : true, "onSale" : true, "price" : 5500 }
...             }
...          }
...       ]
...    )



{
        "acknowledged" : true,
        "deletedCount" : 1,
        "insertedCount" : 1,
        "matchedCount" : 2,
        "upsertedCount" : 0,
        "insertedIds" : {
                "0" : ObjectId("6169dad339fcb911f638b06c")
        },
        "upsertedIds" : {

        }
}

We have successfully learnt how to perform bulkWrite in MongoDB.

Read More: insertMany Function in MongoDB with Nodejs

Conclusion

Learn to use the bulkWrite in MongoDB function to perform bulk actions at once.

Noteworthy References

MongoDB Docs