Difference between revisions of "MongoDB Answers"
Line 1: | Line 1: | ||
[[Main Page]] >> [[MongoDB|MongoDB]] >>[[MongoDB_Workbook|MongoDB Workbook]] >> Exercise Answers | [[Main Page]] >> [[MongoDB|MongoDB]] >>[[MongoDB_Workbook|MongoDB Workbook]] >> Exercise Answers | ||
− | |||
− | |||
<p style="color: red"> | <p style="color: red"> | ||
'''You should only look at the answers once you have attempted them yourself!''' | '''You should only look at the answers once you have attempted them yourself!''' | ||
</p> | </p> | ||
+ | |||
+ | |||
+ | == Answers to Section 2 == | ||
+ | |||
+ | === Exercise 2.1 === | ||
+ | |||
+ | * Compare how you added the above data and how it differs from INSERT records in a relational database | ||
+ | |||
+ | A SQL INSERT statement for department 30 would be: | ||
+ | |||
+ | <pre style="color:blue"> | ||
+ | INSERT INTO dept(deptno, dname, loc) VALUES (30,'SALES','CHICAGO'); | ||
+ | </pre> | ||
+ | |||
+ | Apart from the syntax differences, you could get away with not defining the field names again, for example, this would work too: | ||
+ | |||
+ | <pre style="color:blue"> | ||
+ | INSERT INTO dept VALUES (30,'SALES','CHICAGO'); | ||
+ | </pre> | ||
+ | |||
+ | This is because the table has already been defined, so there is a well defined schema in existence. MongoDB is schemaless, so you need to define some sort of structure as the documents are added. | ||
+ | |||
+ | * Try and add Department 30 | ||
+ | |||
+ | <pre style="color:blue"> | ||
+ | db.dept.insert( | ||
+ | { | ||
+ | _id: 30, | ||
+ | deptno: 30, | ||
+ | dname: "SALES", | ||
+ | loc: "CHICAGO" | ||
+ | }) | ||
+ | </pre> | ||
+ | |||
+ | === Exercise 2.2 === | ||
+ | |||
+ | |||
+ | == Answers to Section 3 == | ||
=== Exercise 3.1 === | === Exercise 3.1 === |
Revision as of 11:15, 13 November 2017
Main Page >> MongoDB >>MongoDB Workbook >> Exercise Answers
You should only look at the answers once you have attempted them yourself!
Answers to Section 2
Exercise 2.1
- Compare how you added the above data and how it differs from INSERT records in a relational database
A SQL INSERT statement for department 30 would be:
INSERT INTO dept(deptno, dname, loc) VALUES (30,'SALES','CHICAGO');
Apart from the syntax differences, you could get away with not defining the field names again, for example, this would work too:
INSERT INTO dept VALUES (30,'SALES','CHICAGO');
This is because the table has already been defined, so there is a well defined schema in existence. MongoDB is schemaless, so you need to define some sort of structure as the documents are added.
- Try and add Department 30
db.dept.insert( { _id: 30, deptno: 30, dname: "SALES", loc: "CHICAGO" })
Exercise 2.2
Answers to Section 3
Exercise 3.1
- Compare how you added the above data and how it differs from INSERT records in a relational database
Think about how this differs from using INSERT statements in Oracle to add the data. An SQL INSERT generally only insert one record at a time to one table.
- Try and add Department 30. Note it has some values for the comm field.
db.deptCollection.insert( { deptno: 30, dname: 'SALES', loc: 'CHICAGO', employees: [ { empno: 7499, ename: 'ALLEN', job: 'SALESMAN', mgr: 7698, hiredate: new Date('1995-02-20'), sal: 1600, comm: 300 }, { empno: 7698, ename: 'BLAKE', job: 'MANAGER', mgr: 7839, hiredate: new Date('1981-05-01'), sal: 2850 }, { empno: 7900, ename: 'JAMES', job: 'CLERK', mgr: 7698, hiredate: new Date('1981-12-03'), sal: 1600 }, { empno: 7654, ename: 'MARTIN', job: 'SALESMAN', mgr: 7698, hiredate: new Date('1993-09-28'), sal: 1250, comm: 1400 }, { empno: 7844, ename: 'TURNER', job: 'SALESMAN', mgr: 7698, hiredate: new Date('1981-09-08'), sal: 1500, comm: 0 }, { empno: 7521, ename: 'WARD', job: 'SALESMAN', mgr: 7698, hiredate: new Date('1994-02-22'), sal: 1250, comm: 500 } ] })
Exercise 3.2
2.2.1 Update the name of department 40 to: COMPUTING
db.deptCollection.update({deptno:40}, {$set: {dname: 'COMPUTING'}})
2.2.1 Update the salary of employee number 7788 in department 20 to 3500
db.deptCollection.update ( { deptno: 20 , "employees.empno": 7788}, { $set: {"employees.$.sal" : 3500} } )
Always check that your update has worked. As a clue, the system should output a WriteResult, telling you how many records were updated:
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Next Step
Return to the Workbook.