MongoDB Update Tutorial – MongoDB Tutorials For Beginners

MongoDB Update Tutorial – MongoDB Tutorials For Beginners from Coding compiler. In the previous MongoDB tutorial, we introduced three-quarter (create, read, update, and delete) operations of CRUD. This chapter, we discuss the operation of our special skipped:  updateUpdate Some unique behaviours, this is why we separate it into this chapter.

MongoDB Update

Now here we discuss on MongoDB update.

Update : Overwrite or $set

In the simplest case,  update there are two parameters: the selector ( where) and the content that needs to be updated. Assuming Roooooodles is getting fat, you will want us to do this:

db.unicorns.update({name: 'Roooooodles'},
    {weight: 590})

(If you have  unicorns broken the collection, it is not the original data, then  remove delete all the data and re-insert all the code in the first chapter.)

Now, if you check the updated records:

db.unicorns.find({name: 'Roooooodles'})

update The first surprise you will find, did not find any documents. Because the second parameter we specified does not use any update options, it replaces the original document. That is,  update first name replace the entire document by finding a document and then overwriting it with a new document (the second parameter). This update is completely different from the SQL  command. In some cases, this is ideal and can be used for some full dynamic updates. However, if you only want to change the value of one or several fields, you should use MongoDB’s  $set operation. Go ahead and let’s update to reset this missing data:

db.unicorns.update({weight: 590}, {$set: {
    name: 'Roooooodles',
    dob: new Date(1979, 7, 18, 18, 44),
    loves: ['apple'],
    gender: 'm',
    vampires: 99}})

This will not overwrite the new field  weight because we didn’t specify it. Let us now execute:

db.unicorns.find({name: 'Roooooodles'})

We got the results we were expecting. So, at the very beginning, the way we correctly update the weight should be:

db.unicorns.update({name: 'Roooooodles'},
    {$set: {weight: 590}})

Update operator

In addition  $set, we can do other interesting things with other update operators. All update operations work on the fields – so you don’t have to worry about the entire document being deleted. For example, it $inc can be used to add a positive/negative value to a field. Suppose Pilot gets two illegal vampire kills points, we can fix it like this:

db.unicorns.update({name: 'Pilot'},
    {$inc: {vampires: -2}})

Assuming that Aurora suddenly has a long tooth, we can loves add a value to her  field by doing  $push :

db.unicorns.update({name: 'Aurora'},
    {$push: {loves: 'sugar'}})

 You can find more information about the available update operators in the Update Operators chapter of the MongoDB manual .

Upserts

With  update one of the biggest surprise is that it fully supports  upserts. The so-called  upsert update, which updates the matching value when it is found in the document, inserts a new value into the document when there is no match, you can understand this. To use upsert we need to write the third parameter to update  {upsert:true}.

One of the most common examples is the website click counter. If we want to save a total number of real-time clicks, we have to first see if there is already a click on the page, and then decide to perform an update or insert based on this. If the upsert option is omitted (or set to false), doing the following will not cause any changes:

db.hits.update({page: 'unicorns'},
    {$inc: {hits: 1}});
db.hits.find();

However, if we add the upsert option, the results will be quite different:

db.hits.update({page: 'unicorns'},
    {$inc: {hits: 1}}, {upsert:true});
db.hits.find();

Since the document with the field page value is  not found  unicorns, a new document is generated for insertion. When we execute this command for the second time, the existing document will be updated and  hits will be increased to 2.

db.hits.update({page: 'unicorns'},
    {$inc: {hits: 1}}, {upsert:true});
db.hits.find();

Batch Updates

About  update the last surprise, the default, only update a single document. So far, all of our examples seem logical. However, if you perform something like this:

db.unicorns.update({},
    {$set: {vaccinated: true }});
db.unicorns.find({vaccinated: true});

You will definitely hope that all your baby unicorns will be vaccinated. To do this, the  multi option needs to be set to true:

db.unicorns.update({},
    {$set: {vaccinated: true }},
    {multi:true});
db.unicorns.find({vaccinated: true});

summary

In this chapter we introduced the basic CRUD operations for collections. We explained in detail  update and its three interesting behaviors. First, if you pass a MongoDB document without an update, MongoDB update will replace the existing document by  default. Therefore, you usually use  $set operations (or other various operations available to modify the document). Second,  update support  upsert operations are very useful when you don’t know if a document exists. Finally, by default,  update only the first matching document is updated, so you will want to use it when you want to update all matching documents  multi .

Leave a Comment