Difference between revisions of "MongoDB Update"
Line 113: | Line 113: | ||
== Next Step == | == Next Step == | ||
− | [[MongoDB_Delete | Deleting]] a document. | + | [[MongoDB_Delete | Deleting]] a document, or collection. |
Revision as of 11:21, 10 November 2016
Main Page >> MongoDB >>MongoDB Workbook >> Updating Collections
Updating a Collection
The format of the update command is:
db.collectionName.update({'keyField': 'value' }, {$set: field: 'newValue' } )
The update() function can be used to update one or more documents. If the change should only apply to one document, the keyField needs to be a field with unique values, to ensure the correct document is updated.
This is similar in SQL to proving the WHERE clause of an UPDATE command.
Alternatively since version 3.2, MongoDB also supports the following functions:
db.collectionName.updateOne(); /* updates a single document that matches a specified filter (even if several documents match the filter) */
db.collectionName.updateMany(); /* updates all documents that matches a specified filter */
db.collectionName.replaceMany(); /* replaces a single document that matches a specified filter (even if several documents match the filter) */
Updating Department 40
To update department 40 to add some employees:
db.deptCollection.update({'deptno':40}, {$set: {'employees': [ { empno: 8888, ename: 'MARY', job: 'LECTURER', mgr: 7566, hiredate: new Date(), sal: 4000 }, { empno: 9999, ename: 'SUSAN', job: 'LECTURER', mgr: 7566, hiredate: new Date(), sal: 3000 } ]} } )
Change the location to Wolverhampton:
db.deptCollection.update({'deptno':40}, {$set: {'loc': 'WOLVERHAMPTON'}})
Check the changes have been made:
db.deptCollection.find({"deptno":40}).pretty()
Adding to the Employees Array
The Employees field is an array of employees and can be manipulated using [array-update] functions.
For example, to add a new employee to department 10, the addToSet operator can be used:
db.deptCollection.update ( { deptno: 10 }, { $addToSet: {employees: { empno: 5555, ename: 'SANJIT', job: 'LECTURER', mgr: 8888, hiredate: new Date(), sal: 3500 } } })
Removing an element from an array
Employees can be deleted using the $pull operator:
db.deptCollection.update ( { deptno: 40 }, { $pull: {employees: { empno: 8888 } } } )
Check the update has worked:
db.deptCollection.find({"deptno":40}).pretty()
Note, when the last element is removed, the employees array will become an empty set.
Updating an element in an array
The positional $ operator identifies an element in an array to update without explicitly specifying the position of the element in the array. This is useful when updating elements within the array, such as the job, or hiredate elements in the Employees array.
For example, to update Sanjit's job title to 'SENIOR LECTURER':
db.deptCollection.update ( { deptno: 10 , "employees.empno": 5555}, { $set: {"employees.$.job" : 'SENIOR LECTURER'} } )
Exercise 2.2
- 2.2.1 Update the name of department 40 to: COMPUTING
- 2.2.1 Update the salary of employee number 7788 in department 20 to 3500
Next Step
Deleting a document, or collection.