Perform Multiple Operations using Bulk in MongoDB

In this lesson, we learn that to execute multiple requests to a MongoDB server efficiently, we can use bulk operations.

We start by initializing a bulk operation that can be either ordered or unordered: initializeOrderedBulOp or initializeUnorderedBulOp

We demonstrate how to add an insertion operation for a document, such as "John Doe," and how to remove another document, "Bob Johnson," from the collection. After executing the bulk operation, we verify the changes made.

Furthermore, we introduce the bulkWrite method for a more streamlined approach to performing multiple operations in one command, allowing us to specify each operation's details explicitly while managing both insertions and deletions effectively.

Share with a coworker

Transcript

[00:00] In order to perform multiple requests within the same request to a Mongo server, we need to create what is called a bulk operation to Mongo. So we'll start with initializing a bulk operation which is either ordered or unordered. And in order to put the operations into the bulk, we need to store the bulk inside a variable. So we create a bulk and now we can put an insertion operation, which is simply going to be the same as insert one or insert many so let's create an Document which is going to include let's say John Doe. So this is going to be a one bulk So here we can see what is going to be updated over here.

[00:47] And let's also create a bulk. Now let's remove something. But before we actually remove something, we need to find it. So this is the syntax. So within the bulk we find And let's remove our favorite one.

[01:03] This is going to be Bob Johnson again, the last time. And yep, this is the document. Now, after we have found it, we're going to run the remove one. Now, this is the amount of the operations in the batch. And what we can do right now is to run the bulk execute.

[01:25] So we can see what has been done here. So acknowledge true, inserted count, inserted IDs, blah, blah, blah. Now, if we take a look at our collection employees let's see what is the last element we would see that there is John Doe. Also if we try to search for the name Bob Johnson we should see that there is no such document within the collection, which is absolutely true. There is also a possibility to make the bulk operations more concise, and that is to execute a bulk write.

[02:01] So we're going to make it a single statement, though the code is going to be slightly more verbose. So bulk write accepts an array of operations and the operations themselves will be slightly more verbose. And that is we're going to create an object where there would be keys such as insert one, delete one and so on and so forth. So here we're not even putting what the document to be inserted is going to be but we also need to specify that hey this is the document. Why?

[02:34] Because the insert one, replace one, update one, etc. They need to provide the same schema in this case and sometimes we will use what is the document, other times what is the replacement, yet another one what is that filter and so on and so forth so here this is slightly verbose so the document that we're going to insert is going to be name and here make it slightly different and that is going to be Jane Doe So this is the first one as you can see ending, ending, ending curly braces. And here what we're going to do is to delete one and we know that there is a document that has the name Ada Lovelace. So we're going to use it, Ada Lovelace. And we're going to run these two in a bulk.

[03:26] And as you can see here, we're using the document, here we're using the filter, so it's slightly more verbose, although we're running it in a single request. So we have a inserted count one and deleted count one. So let's take a look at the document that we have right now. So there is John Doe and Jane Doe. This is perfect.

[03:50] And let's now try to see whether Ada Lovelace is still with us. And we can see that no, the document has been deleted. You