Difference between revisions of "MongoDB CreateCollection"
From mi-linux
Jump to navigationJump to searchLine 23: | Line 23: | ||
job: 'MANAGER', | job: 'MANAGER', | ||
mgr: 7839, | mgr: 7839, | ||
− | hiredate: new Date('1989- | + | hiredate: new Date('1989-06-09'), |
sal: 2450 | sal: 2450 | ||
}, | }, | ||
Line 30: | Line 30: | ||
ename: 'KING', | ename: 'KING', | ||
job: 'PRESIDENT', | job: 'PRESIDENT', | ||
− | hiredate: new Date('1980- | + | hiredate: new Date('1980-11-17'), |
sal: 5000 | sal: 5000 | ||
}, | }, | ||
Line 38: | Line 38: | ||
job: 'CLERK', | job: 'CLERK', | ||
mgr: 7782, | mgr: 7782, | ||
− | hiredate: new Date('1985- | + | hiredate: new Date('1985-01-23'), |
sal: 1300 | sal: 1300 | ||
} | } | ||
Line 66: | Line 66: | ||
job: 'ANALYST', | job: 'ANALYST', | ||
mgr: 7566, | mgr: 7566, | ||
− | hiredate: new Date('1991- | + | hiredate: new Date('1991-12-03'), |
sal: 3000 | sal: 3000 | ||
}, | }, | ||
Line 74: | Line 74: | ||
job: 'MANAGER', | job: 'MANAGER', | ||
mgr: 7839, | mgr: 7839, | ||
− | hiredate: new Date('1991- | + | hiredate: new Date('1991-04-02'), |
sal: 2975 | sal: 2975 | ||
}, | }, | ||
Line 82: | Line 82: | ||
job: 'ANALYST', | job: 'ANALYST', | ||
mgr: 7566, | mgr: 7566, | ||
− | hiredate: new Date('2015- | + | hiredate: new Date('2015-10-16'), |
sal: 3000 | sal: 3000 | ||
} | } | ||
Line 104: | Line 104: | ||
* every opening bracket has a closing bracket | * every opening bracket has a closing bracket | ||
* the insert statement uses round brackets: () | * 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 quotes: 'myString' | * strings are enclosed in single quotes: 'myString' | ||
− | * date strings are enclosed in single quotes and the format is YYYY- | + | * date strings are enclosed in single quotes and the format is YYYY-MM-DD e.g., Date('2016-10-10') |
=== Exercise 2.1 === | === Exercise 2.1 === | ||
Try and add Department 30. Note it has some values for the comm field. | Try and add Department 30. Note it has some values for the comm field. |
Revision as of 20:20, 18 October 2016
Main Page >> MongoDB >>MongoDB Workbook >> Create Collections
Create a Collection
If a collection does not exist, MongoDB creates the collection when you first store data for that collection.
You can create one and set some options:
db.createCollection('deptCollection', {autoIndexID : true})
The following example will create a collection representing 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 } ] } )
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 } ] } )
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: ()
- 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 quotes: 'myString'
- date strings are enclosed in single quotes and the format is YYYY-MM-DD e.g., Date('2016-10-10')
Exercise 2.1
Try and add Department 30. Note it has some values for the comm field.