By Nicholas Brown.
<< Back To MongoDB Write Operations
MongoDB Update Operations
We now have three fruits in the database, but the serving size for the orange record is not the same as that of the other fruits. Therefore, we cannot do a fair comparison of these fruits/vegetables’ potassium content, because there is more orange than there is of the others.
To correct this we can update the record using the ‘$set‘ operator as shown below. Bear in mind that you need to use this instead of the ‘insert’ command, because the insert command will just create another record, resulting in duplicate documents.
db.fruits.updateOne( {Name: 'Orange'}, { $set: {PotassiumContent: 181, ServingSize: 100} } );
You could do this in SQL by typing the following:
UPDATE fruits
SET potassiumcontent=181, servingsize=100
WHERE name=’Orange’;