Difference between revisions of "MongoDB Update"

From mi-linux
Jump to navigationJump to search
Line 3: Line 3:
 
== Updating a Collection ==
 
== Updating a Collection ==
  
To add an employee to department 40:
+
The format of the update command is:
 +
 
 +
<pre style="color:blue">
 +
db.collectionName.update({'KeyField': 'value' },
 +
  {$set: fieldName: 'newValue' }
 +
)
 +
</pre>
 +
 
 +
=== Updating Department 40 ===
 +
 
 +
To update department 40 to add an employee:
  
 
  db.deptCollection.update({'deptno':40},  
 
  db.deptCollection.update({'deptno':40},  

Revision as of 10:44, 21 October 2016

Main Page >> MongoDB >>MongoDB Workbook >> Updating Collections

Updating a Collection

The format of the update command is:

 db.collectionName.update({'KeyField': 'value' }, 
  {$set: fieldName: 'newValue' }
 )

Updating Department 40

To update department 40 to add an employee:

db.deptCollection.update({'deptno':40}, 
 {$set: 
   {'employees': [   
     {
      empno: 8888,
      ename: 'MARY',
      job: 'LECTURER',
      mgr: 7566,
      hiredate: new Date(),
      sal: 4000
     } 
   ]}
 }
)

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()