Let's have a look at updating documents. And for updating the documents, let's take a different collection. And I have another JSON array document called products dot JSON. Let's import the same thing into our MongoDB database. To do so let's execute the Mongo import with JSON array. And the database that we want to use is the test dB.
And the collection that we want to use is the same name as the product. So I can just type your products dot JSON. Now since we didn't mention the collection name, it's going to take the base name of this file, which happens to be a product. So let's execute and it says imported 85 documents. Now one thing you can observe this time is that if I run the command once more, it's going to result into errors. And that's because if you observe, we already have underscore ID property for every animal Comment.
If you try to enter it again with the same underscore ID, we get the error. So now that we have got all the documents inserted, let's have a look at what exactly we have as a use test dB. Show collections. And this time, I want to say DB dot products dot find dot pretty. Let's say we want to modify a data for one of these products. For example, let's say the name of this particular product for 19.
Instead of branch all I want to change this to eggplant. And to do that, we would be using the update command. So we'll say DB dot products dot update. So the update takes few parameters. The first parameter of this update function is the query so the query happens to be underscore ID Is 19 The second is the document that you want to view. Now, if I just say that I want to change the name of this from brinjal to eggplant and press Enter.
Now it says that number of documents matched is one number of absurd documents is zero and number of modified documents is one. And if I go and have a look at whether it has updated or not, db dot products dot find dot pretty. And you should see that the product number 19 is changed, but not as we expected. We wanted the name to be changed, but it has replaced the entire document. So this was a bad idea. So in order to change only one or two properties of an entire document, we should be using an operator called set.
For example Let's do the same thing for this one, except that instead of the brand fresher, I want to change this to brand Molnar. And to do so this time, I'm going to use VB dot products dot update, and say the same query this time underscore ID is 20. And then the document that we want to update happens to be dollar set. And then we say what are the properties that we want to set. So I'll say here, brand happens to be Milan. And if I want, I can also add another property which is currently not existing.
For example, I may say that details and I can say or something like this, which I don't have any details at this time. So I close the bracket for the set operators, and the close the bracket for the modifying document, and that's the end of the update function so I can execute the same thing. Again, the same result it says number of days. mismatched is one number of observed a document is zero and modified document is one. Unlike the previous one now if I execute the find method to retrieve all the values, you will see that the document 20 is intact except the brand is changed. A new property called details is added