Difference between revisions of "MongoDB InsertNestedData"
(4 intermediate revisions by the same user not shown) | |||
Line 7: | Line 7: | ||
The format for the INSERT command is still the same, but this time one of our key values will be an array to hold the employee details: | The format for the INSERT command is still the same, but this time one of our key values will be an array to hold the employee details: | ||
− | <pre style="color: | + | <pre style="color: purple"> |
db.collectionName.insert( | db.collectionName.insert( | ||
{ | { | ||
Line 158: | Line 158: | ||
* Compare how you added the above data and how it differs from INSERT records in a relational database | * Compare how you added the above data and how it differs from INSERT records in a relational database | ||
− | * | + | * Add Department 30 and its employees as a nested array. |
+ | |||
+ | The department document has the following key/values: deptno: 30, dname: SALES and loc: CHICAGO | ||
+ | |||
+ | The employee details are as follows: | ||
+ | |||
+ | <table class=emp cellspacing=0 border = "1" width= "50%"> | ||
+ | <tr> | ||
+ | <th scope="col">EMPNO</th> | ||
+ | <th scope="col">ENAME</th> | ||
+ | <th scope="col">JOB</th> | ||
+ | <th scope="col">MGR</th> | ||
+ | <th scope="col">HIREDATE</th> | ||
+ | <th scope="col">SAL</th> | ||
+ | <th scope="col">COMM</th> | ||
+ | <th scope="col">DEPTNO</th> | ||
+ | </tr> | ||
+ | <tr><td align="right">7499</td> | ||
+ | <td>ALLEN</td> | ||
+ | <td>SALESMAN</td> | ||
+ | <td align="right">7698</td> | ||
+ | <td>20-FEB-95</td> | ||
+ | <td align="right">1600</td> | ||
+ | <td align="right">300</td> | ||
+ | <td align="right">30</td> | ||
+ | </tr> | ||
+ | <tr> | ||
+ | <td align="right">7698</td> | ||
+ | <td>BLAKE</td> | ||
+ | <td>MANAGER</td> | ||
+ | <td align="right">7839</td> | ||
+ | <td>01-MAY-81</td> | ||
+ | <td align="right">2850</td> | ||
+ | <td align="right"> </td> | ||
+ | <td align="right">30</td> | ||
+ | </tr> | ||
+ | <tr> | ||
+ | <td align="right">7900</td> | ||
+ | <td>JAMES</td> | ||
+ | <td>CLERK</td> | ||
+ | <td align="right">7698</td> | ||
+ | <td>03-DEC-81</td> | ||
+ | <td align="right">950</td> | ||
+ | <td align="right"> </td> | ||
+ | <td align="right">30</td> | ||
+ | </tr> | ||
+ | <tr> | ||
+ | <td align="right">7654</td> | ||
+ | <td>MARTIN</td> | ||
+ | <td>SALESMAN</td> | ||
+ | <td align="right">7698</td> | ||
+ | <td>28-SEP-93</td> | ||
+ | <td align="right">1250</td> | ||
+ | <td align="right">1400</td> | ||
+ | <td align="right">30</td> | ||
+ | </tr> | ||
+ | <tr> | ||
+ | <td align="right">7844</td> | ||
+ | <td>TURNER</td> | ||
+ | <td>SALESMAN</td> | ||
+ | <td align="right">7698</td> | ||
+ | <td>08-SEP-81</td> | ||
+ | <td align="right">1500</td> | ||
+ | <td align="right">0</td> | ||
+ | <td align="right">30</td> | ||
+ | </tr> | ||
+ | <tr> | ||
+ | <td align="right">7521</td> | ||
+ | <td>WARD</td> | ||
+ | <td>SALESMAN</td> | ||
+ | <td align="right">7698</td> | ||
+ | <td>22-FEB-94</td> | ||
+ | <td align="right">1250</td> | ||
+ | <td align="right">500</td> | ||
+ | <td align="right">30</td> | ||
+ | </tr> | ||
+ | </table> | ||
== Next Step == | == Next Step == | ||
[[MongoDB_QueryNestedData|Querying]] nested data in the collection | [[MongoDB_QueryNestedData|Querying]] nested data in the collection |
Latest revision as of 20:21, 13 November 2017
Main Page >> MongoDB >>MongoDB Workbook >> Insert Nested Data
Inserting Nested data
For this exercise we are going to nest the Employee details within the Department, rather than having two separate collections as seen in the previous section.
The format for the INSERT command is still the same, but this time one of our key values will be an array to hold the employee details:
db.collectionName.insert( { key_1: 'value1', key_n: valueN, keyArrayName: [ { nestedKey_1: 'value1', nestedKey_n: valueN } ] } )
Note:
- character and date values must be enclosed in matching single (') or double quotes (").
- numeric values do not need quotes.
- key/value pairs are comma-separated and the value can be a nested document or array.
- each record is enclosed in curly brackets: {}
- the array that contains the nested information will be in square brackets: []
- if you are getting error messages when creating a collection check that you have the right sort of brackets in the correct place!
Department 10
The following examples will represent the DEPT/EMP tables seen in the Oracle Sample Data.
Add department 10 and it's employees:
db.deptCollection.insert( { deptno: 10, dname: 'ACCOUNTING', loc: 'NEW YORK', employees: [ { empno: 7782, ename: 'CLARK', job: 'MANAGER', mgr: 7839, hiredate: new Date('1989-06-09'), sal: 2450 }, { empno:7839, ename: 'KING', job: 'PRESIDENT', hiredate: new Date('1980-11-17'), sal: 5000 }, { empno: 7934, ename: 'MILLER', job: 'CLERK', mgr: 7782, hiredate: new Date('1985-01-23'), sal: 1300 } ] } )
Assuming you have no error messages, the system should respond with:
WriteResult({ "nInserted" : 1 })
Department 20
Next add department 20:
db.deptCollection.insert( { deptno: 20, dname: 'RESEARCH', loc: 'DALLAS', employees: [ { empno: 7876, ename: 'ADAMS', job: 'CLERK', mgr: 7788, hiredate: new Date(), sal: 1100 }, { empno: 7902, ename: 'FORD', job: 'ANALYST', mgr: 7566, hiredate: new Date('1991-12-03'), sal: 3000 }, { empno: 7066, ename: 'JONES', job: 'MANAGER', mgr: 7839, hiredate: new Date('1991-04-02'), sal: 2975 }, { empno: 7788, ename: 'SCOTT', job: 'ANALYST', mgr: 7566, hiredate: new Date('2015-10-16'), sal: 3000 } ] } )
Department 40
Next department 40, which has no employees currently:
db.deptCollection.insert( { deptno: 40, dname: 'OPERATIONS', loc: 'BOSTON' } )
Check the Collection
Now check if you have any collections:
show collections
Things to note
If you are getting errors, check carefully that:
- every opening bracket has an appropriate closing bracket:
- the insert statement uses round brackets: ()
- an array uses square brackets: []
- a collection uses curly brackets: {}
- each element in a nested collection will also use curly brackets
- each key:value pair are separated by commas, except for the last item
- strings are enclosed in single or double quotes, e.g., 'myString', or "myString"
- date strings are enclosed in single/double quotes and the format is 'yyyy-mm-dd' e.g., Date('2016-10-10')
- use the Date() constructor to create a date datatype
- date can also be a datetime, e.g., new Date("<yyyy-mm-ddThh:mm:ss>")
- new Date() will return the current date
Exercise 3.1
- Compare how you added the above data and how it differs from INSERT records in a relational database
- Add Department 30 and its employees as a nested array.
The department document has the following key/values: deptno: 30, dname: SALES and loc: CHICAGO
The employee details are as follows:
EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO |
---|---|---|---|---|---|---|---|
7499 | ALLEN | SALESMAN | 7698 | 20-FEB-95 | 1600 | 300 | 30 |
7698 | BLAKE | MANAGER | 7839 | 01-MAY-81 | 2850 | 30 | |
7900 | JAMES | CLERK | 7698 | 03-DEC-81 | 950 | 30 | |
7654 | MARTIN | SALESMAN | 7698 | 28-SEP-93 | 1250 | 1400 | 30 |
7844 | TURNER | SALESMAN | 7698 | 08-SEP-81 | 1500 | 0 | 30 |
7521 | WARD | SALESMAN | 7698 | 22-FEB-94 | 1250 | 500 | 30 |
Next Step
Querying nested data in the collection