| Start | ||||||
|---|---|---|---|---|---|---|
$set operator to update specified field(s) in document(s) I.OverviewA significant difference between MongoDB & RDBMS is MongoDB has many kinds of operators. One of them is update operator, which is used in update statements. II.What happen if we don't use update operators?Suppose we have a student collection to store student information(Table view):
One day you get a job that need to change Tom's gender from "M" to "F". That's easy, right? So you write below statement very quickly based on your RDBMS experience: Let's see what is the result:
We lost Tom's age & name! From this example, we can know that the whole document will be overrided if without any update operator in update statement. This is the default behavior of MongoDB. III.$set operatorIf we want to change only the 'sex' field in Tom's document, we can use The value of So, the result is correct now:
Also, if you want to change both 'sex' and 'age' at the same time, you can append them to Syntax
Parameters
RemarksReference for $set operator: $set on offical website (opens new window) |
Read Article
https://develop.horje.com/learn/1434/reference