|
|
(18 intermediate revisions by the same user not shown) |
Line 2: |
Line 2: |
| == Create a Collection == | | == Create a Collection == |
| | | |
− | If a collection does not exist, MongoDB creates the collection when you first store data for that collection. | + | If a collection does not exist, MongoDB will automatically create the collection when you first store data for that collection. |
| | | |
− | You can create one and set some options:
| + | When you first use MongoDB you will find you have no collections: |
| | | |
− | db.createCollection('deptCollection', {autoIndexID : true}) | + | show collections |
| | | |
− | The following example will create a collection representing the DEPT/EMP tables seen in the [[Oracle_Sample_Data|Oracle Sample Data]].
| + | When you insert data in the next step, a collection will automatically be created for you. |
| | | |
− | Add department 10 and it's employees:
| + | == Next Step == |
| | | |
− | db.deptCollection.insert(
| + | [[MongoDB_InsertData|Inserting]] data into the collection |
− | {
| |
− | deptno: 10,
| |
− | dname: 'ACCOUNTING',
| |
− | loc: 'NEW YORK',
| |
− | employees: [
| |
− | {
| |
− | empno: 7782,
| |
− | ename: 'CLARK',
| |
− | job: 'MANAGER',
| |
− | mgr: 7839,
| |
− | hiredate: new Date('1989-JUN-09'),
| |
− | sal: 2450
| |
− | },
| |
− | {
| |
− | empno:7839,
| |
− | ename: 'KING',
| |
− | job: 'PRESIDENT',
| |
− | hiredate: new Date('1980-NOV-17'),
| |
− | sal: 5000
| |
− | },
| |
− | {
| |
− | empno: 7934,
| |
− | ename: 'MILLER',
| |
− | job: 'CLERK',
| |
− | mgr: 7782,
| |
− | hiredate: new Date('1985-JAN-23'),
| |
− | sal: 1300
| |
− | }
| |
− | ]
| |
− | }
| |
− | )
| |
− | | |
− | 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-DEC-03'),
| |
− | sal: 3000
| |
− | },
| |
− | {
| |
− | empno: 7066,
| |
− | ename: 'JONES',
| |
− | job: 'MANAGER',
| |
− | mgr: 7839,
| |
− | hiredate: new Date('1991-APR-02'),
| |
− | sal: 2975
| |
− | },
| |
− | {
| |
− | empno: 7788,
| |
− | ename: 'SCOTT',
| |
− | job: 'ANALYST',
| |
− | mgr: 7566,
| |
− | hiredate: new Date('2015-OCT-16'),
| |
− | sal: 3000
| |
− | }
| |
− | ]
| |
− | }
| |
− | )
| |
− | | |
− | Next department 40, which has no employees currently:
| |
− | | |
− | db.deptCollection.insert(
| |
− | {
| |
− | deptno: 40,
| |
− | dname: 'OPERATIONS',
| |
− | loc: 'BOSTON'
| |
− | }
| |
− | }
| |
− | | |
− | === Things to note ===
| |
− | | |
− | If you are getting errors, check carefully that:
| |
− | * every opening bracket has a closing bracket
| |
− | * the insert statement uses round brackets: ()
| |
− | * collections use curly brackets: {}
| |
− | * strings are enclosed in single quotes: 'myString'
| |
− | * date strings are enclosed in single quotes and the format is YYYY-MON-DD e.g., Date('2016-Oct-10')
| |
− | | |
− | === Exercise 2.1 ===
| |
− | | |
− | Try and add Department 30. Note it has some values for the comm field.
| |
Main Page >> MongoDB >>MongoDB Workbook >> Create Collections
Create a Collection
If a collection does not exist, MongoDB will automatically create the collection when you first store data for that collection.
When you first use MongoDB you will find you have no collections:
show collections
When you insert data in the next step, a collection will automatically be created for you.
Next Step
Inserting data into the collection