You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.4 KiB

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

# Mongo - Update
## Lesson Objectives
1. Explain how to replace a record
1. Explain how to update certain values for a record
1. Explain update operators
1. Explain upserts
1. Explain multiple updates
## Explain how to replace a record
1.`db.employees.update({name:'Roooooodles'}, {weight: 590})`
## Explain how to update certain values for a record
```
db.employees.update(
{weight: 590},
{$set: {
name: 'Roooooodles',
dob: new Date(1979, 7, 18, 18, 44),
loves: ['apple'],
gender: 'm',
salary: 99}})
```
## Explain update operators
1. `db.employees.update({name: 'Pilot'}, {$inc: {salary: -2}})`
1. `db.employees.update({name: 'Pilot'}, {$mul: {salary: (1/2)}})`
1. `db.employees.update({name: 'Aurora'}, {$push: {loves: 'sugar'}})`
1. `db.employees.update({name: 'Aurora'}, {$pop: 1})`
1. `db.employees.update({name: 'Aurora'}, {$unset: {loves: ''}})`
1. `db.employees.update({name: 'Aurora'}, {$rename: {wrong_field_name : 'correct_field_name'}})`
1. http://docs.mongodb.org/manual/reference/operator/update/#update-operators
## Explain multiple updates
1. update() updates first entry
```
db.employees.update(
{},
{$set: {vaccinated: true}},
{multi:true});
db.employees.find({vaccinated: true});
```
## Explain upserts
1. `db.hits.update({page: 'employees'}, {$inc: {hits: 1}}); db.hits.find();`
1. `db.hits.update({page: 'employees'}, {$inc: {hits: 1}}, {upsert:true}); db.hits.find();`