Difference between revisions of "MongoDB InsertData"
(38 intermediate revisions by 3 users not shown) | |||
Line 5: | Line 5: | ||
The format for the INSERT command is: | The format for the INSERT command is: | ||
− | <pre style="color: | + | <pre style="color: purple"> |
db.collectionName.insert( | db.collectionName.insert( | ||
{ | { | ||
key_1: 'value1', | key_1: 'value1', | ||
− | key_n: valueN | + | key_n: 'valueN' |
} | } | ||
) | ) | ||
</pre> | </pre> | ||
− | Note: | + | '''Note:''' |
* character and date values must be enclosed in matching single (') or double quotes ("). | * character and date values must be enclosed in matching single (') or double quotes ("). | ||
* numeric values do not need quotes. | * numeric values do not need quotes. | ||
Line 30: | Line 30: | ||
db.dept.insert( | db.dept.insert( | ||
{ | { | ||
− | _id: 10, | + | _id: 10, |
deptno: 10, | deptno: 10, | ||
dname: "ACCOUNTING", | dname: "ACCOUNTING", | ||
Line 48: | Line 48: | ||
db.dept.insert( | db.dept.insert( | ||
{ | { | ||
− | _id: 20, | + | _id: 20, |
deptno: 20, | deptno: 20, | ||
dname: "RESEARCH", | dname: "RESEARCH", | ||
loc: "DALLAS" | loc: "DALLAS" | ||
− | }) | + | }) |
=== Department 40 === | === Department 40 === | ||
Line 60: | Line 60: | ||
db.dept.insert( | db.dept.insert( | ||
{ | { | ||
− | _id: 40, | + | _id: 40, |
deptno: 40, | deptno: 40, | ||
dname: "OPERATIONS", | dname: "OPERATIONS", | ||
loc: "BOSTON" | loc: "BOSTON" | ||
− | }) | + | }) |
+ | |||
+ | == Object IDs == | ||
+ | |||
+ | Object ids can be used to provide a unique value in a collection, that is equivalent to a primary key field in relational databases. They are either system generated and are guaranteed to be unique, or can be created by the user as seen above. If user defined, the user must provide unique values within the collection. | ||
+ | |||
+ | For example, try and add another document with the id of 10: | ||
+ | |||
+ | db.dept.insert( | ||
+ | { | ||
+ | _id: 10, | ||
+ | deptno: 50, | ||
+ | dname: "TEST", | ||
+ | loc: "WOLVERHAMPTON" | ||
+ | }) | ||
+ | |||
+ | You should get an error message along the lines of: ''...duplicate key error collection: dbYourStudentNumber.dept....''' | ||
+ | |||
+ | Whereas if you add a new document with an existing deptno, but different object ID, it will quite happily accept it: | ||
+ | |||
+ | db.dept.insert( | ||
+ | { | ||
+ | _id: 50, | ||
+ | deptno: 40, | ||
+ | dname: "OPERATIONS V2", | ||
+ | loc: "BOSTON" | ||
+ | }) | ||
+ | |||
+ | |||
=== Things to note === | === Things to note === | ||
Line 78: | Line 106: | ||
* 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, which has the following key/values: _id: 30, deptno: 30, dname: SALES and loc: CHICAGO |
+ | == Employee data == | ||
− | + | The following examples will create a ''emp'' collection that will represent the EMP table seen in the [[Oracle_Sample_Data|Oracle Sample Data]]. | |
+ | |||
+ | No object id (_id) is included this time, so you can compare the previous examples against the object id generated automatically in the documents below. | ||
− | More than one record can be added at a time. The following examples will | + | More than one record can be added at a time. The following examples will add several employees for each department. |
=== Department 10 Employees === | === Department 10 Employees === | ||
db.emp.insert( [ | db.emp.insert( [ | ||
− | + | { | |
empno: 7782, | empno: 7782, | ||
ename: 'CLARK', | ename: 'CLARK', | ||
Line 117: | Line 148: | ||
) | ) | ||
+ | This time the results returned will be along the lines of: | ||
+ | |||
+ | <pre style="color: blue"> | ||
+ | BulkWriteResult({ | ||
+ | "writeErrors" : [ ], | ||
+ | "writeConcernErrors" : [ ], | ||
+ | "nInserted" : 3, | ||
+ | "nUpserted" : 0, | ||
+ | "nMatched" : 0, | ||
+ | "nModified" : 0, | ||
+ | "nRemoved" : 0, | ||
+ | "upserted" : [ ] | ||
+ | </pre> | ||
=== Department 20 Employees === | === Department 20 Employees === | ||
db.emp.insert( [ | db.emp.insert( [ | ||
− | |||
{ | { | ||
empno: 7876, | empno: 7876, | ||
Line 159: | Line 202: | ||
} | } | ||
] | ] | ||
− | |||
) | ) | ||
+ | |||
+ | === Date data types === | ||
+ | |||
+ | Dates have been included in the above: | ||
+ | * 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 2.2 == | ||
+ | |||
+ | * Add the employees for Department 30. | ||
+ | |||
+ | Remember this time, some employees will have a commission (COMM): | ||
+ | |||
+ | <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> | ||
+ | |||
+ | If you add all the documents in one insert statement, you will get a message along the lines of: | ||
+ | |||
+ | <pre style="color:blue"> | ||
+ | BulkWriteResult({ | ||
+ | "writeErrors" : [ ], | ||
+ | "writeConcernErrors" : [ ], | ||
+ | "nInserted" : 6, | ||
+ | "nUpserted" : 0, | ||
+ | "nMatched" : 0, | ||
+ | "nModified" : 0, | ||
+ | "nRemoved" : 0, | ||
+ | "upserted" : [ ] | ||
+ | }) | ||
+ | </pre> | ||
== Next Step == | == Next Step == | ||
[[MongoDB_QueryData|Querying]] the collection | [[MongoDB_QueryData|Querying]] the collection |
Latest revision as of 17:52, 12 February 2020
Main Page >> MongoDB >>MongoDB Workbook >> Insert Data
Inserting data
The format for the INSERT command is:
db.collectionName.insert( { key_1: 'value1', key_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 (no comma needed after the last pair)
- each record is enclosed in curly brackets: {}
Department Data
The following examples will create a dept collection that will represent the DEPT table seen in the Oracle Sample Data. We will also include an object id (_id) for each record.
Department 10
Add department 10:
db.dept.insert( { _id: 10, deptno: 10, dname: "ACCOUNTING", loc: "NEW YORK" })
Assuming you have no error messages, the system should respond with:
WriteResult({ "nInserted" : 1 })
Department 20
Next add department 20:
db.dept.insert( { _id: 20, deptno: 20, dname: "RESEARCH", loc: "DALLAS" })
Department 40
Next department 40:
db.dept.insert( { _id: 40, deptno: 40, dname: "OPERATIONS", loc: "BOSTON" })
Object IDs
Object ids can be used to provide a unique value in a collection, that is equivalent to a primary key field in relational databases. They are either system generated and are guaranteed to be unique, or can be created by the user as seen above. If user defined, the user must provide unique values within the collection.
For example, try and add another document with the id of 10:
db.dept.insert( { _id: 10, deptno: 50, dname: "TEST", loc: "WOLVERHAMPTON" })
You should get an error message along the lines of: ...duplicate key error collection: dbYourStudentNumber.dept....'
Whereas if you add a new document with an existing deptno, but different object ID, it will quite happily accept it:
db.dept.insert( { _id: 50, deptno: 40, dname: "OPERATIONS V2", loc: "BOSTON" })
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: ()
- a collection uses 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"
Exercise 2.1
- Compare how you added the above data and how it differs from INSERT records in a relational database
- Add Department 30, which has the following key/values: _id: 30, deptno: 30, dname: SALES and loc: CHICAGO
Employee data
The following examples will create a emp collection that will represent the EMP table seen in the Oracle Sample Data.
No object id (_id) is included this time, so you can compare the previous examples against the object id generated automatically in the documents below.
More than one record can be added at a time. The following examples will add several employees for each department.
Department 10 Employees
db.emp.insert( [ { empno: 7782, ename: 'CLARK', job: 'MANAGER', mgr: 7839, hiredate: new Date('1989-06-09'), sal: 2450, deptno: 10 }, { empno:7839, ename: 'KING', job: 'PRESIDENT', hiredate: new Date('1980-11-17'), sal: 5000, deptno: 10 }, { empno: 7934, ename: 'MILLER', job: 'CLERK', mgr: 7782, hiredate: new Date('1985-01-23'), sal: 1300, deptno: 10 } ] )
This time the results returned will be along the lines of:
BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 3, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ]
Department 20 Employees
db.emp.insert( [ { empno: 7876, ename: 'ADAMS', job: 'CLERK', mgr: 7788, hiredate: new Date(), sal: 1100, deptno: 20 }, { empno: 7902, ename: 'FORD', job: 'ANALYST', mgr: 7566, hiredate: new Date('1991-12-03'), sal: 3000, deptno: 20 }, { empno: 7066, ename: 'JONES', job: 'MANAGER', mgr: 7839, hiredate: new Date('1991-04-02'), sal: 2975, deptno: 20 }, { empno: 7788, ename: 'SCOTT', job: 'ANALYST', mgr: 7566, hiredate: new Date('2015-10-16'), sal: 3000, deptno: 20 } ] )
Date data types
Dates have been included in the above:
- 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 2.2
- Add the employees for Department 30.
Remember this time, some employees will have a commission (COMM):
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 |
If you add all the documents in one insert statement, you will get a message along the lines of:
BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 6, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] })
Next Step
Querying the collection