Difference between revisions of "MongoDB Update"

From mi-linux
Jump to navigationJump to search
Line 33: Line 33:
 
=== Updating Department 40 ===
 
=== Updating Department 40 ===
  
To update department 40 to add an employee:
+
To update department 40 to add some employees:
  
 
  db.deptCollection.update({'deptno':40},  
 
  db.deptCollection.update({'deptno':40},  
  {$set:  
+
{$set:  
    {'employees': [   
+
  {'employees': [   
      {
+
    {
      empno: 8888,
+
      empno: 8888,
      ename: 'MARY',
+
      ename: 'MARY',
      job: 'LECTURER',
+
      job: 'LECTURER',
      mgr: 7566,
+
      mgr: 7566,
      hiredate: new Date(),
+
      hiredate: new Date(),
      sal: 4000
+
      sal: 4000
       }  
+
    },
    ]}
+
    {
  }
+
      empno: 9999,
 +
      ename: 'SUSAN',
 +
      job: 'LECTURER',
 +
      mgr: 7566,
 +
       hiredate: new Date(),
 +
      sal: 3000
 +
    }  
 +
  ]}
 +
}
 
  )
 
  )
  
Line 60: Line 68:
  
  
The salary should have been 3000. There is an issue with the current design, in that the whole array has to be replaced:
+
The salary should have been 3000 for both lecturers. There is an issue with the current design, in that the whole array has to be redefined. What happens if we try just the following:
  
  
Line 77: Line 85:
 
   }
 
   }
 
  )
 
  )
 +
 +
Check what changes have been made to Department 40. Is this what you expected?
 +
 +
The section on ObjectIds will revisit how our design can be improved.
  
 
== Exercise 2.2 ==
 
== Exercise 2.2 ==

Revision as of 12:57, 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: 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()


The salary should have been 3000 for both lecturers. There is an issue with the current design, in that the whole array has to be redefined. What happens if we try just the following:


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

Check what changes have been made to Department 40. Is this what you expected?

The section on ObjectIds will revisit how our design can be improved.

Exercise 2.2

  • 2.2.1 Update the name of department 40 to: COMPUTING
  • 2.2.1 Update the ename of employee number xxx to xxx

Next Step

Deleting a document.