Difference between revisions of "MongoDB Documents"

From mi-linux
Jump to navigationJump to search
Line 10: Line 10:
  
 
  db.deptCollection.insert(  
 
  db.deptCollection.insert(  
{
+
  {
deptno: 10,  
+
    deptno: 10,  
dname: 'ACCOUNTING',
+
    dname: 'ACCOUNTING',
loc: 'NEW YORK',
+
    loc: 'NEW YORK',
employees: [
+
    employees: [
{
+
      {
empno: 7782,
+
        empno: 7782,
ename: 'CLARK',
+
        ename: 'CLARK',
job: 'MANAGER',
+
        job: 'MANAGER',
mgr: 7839,
+
        mgr: 7839,
hiredate: new Date('1989-JUN-09'),
+
        hiredate: new Date('1989-JUN-09'),
sal: 2450
+
        sal: 2450
},
+
      },
{
+
      {
empno:7839,
+
        empno:7839,
ename: 'KING',
+
        ename: 'KING',
job: 'PRESIDENT',
+
        job: 'PRESIDENT',
hiredate: new Date('1980-NOV-17'),
+
        hiredate: new Date('1980-NOV-17'),
sal: 5000
+
        sal: 5000
},
+
      },
{
+
      {
empno: 7934,
+
        empno: 7934,
ename: 'MILLER',
+
        ename: 'MILLER',
job: 'CLERK',
+
        job: 'CLERK',
mgr: 7782,
+
        mgr: 7782,
hiredate: new Date('1985-JAN-23'),
+
        hiredate: new Date('1985-JAN-23'),
sal: 1300
+
        sal: 1300
}
+
      }
]
+
    ]
}  
+
    }  
)
+
  )

Revision as of 19:39, 18 October 2016

Main Page >> MongoDB >>MongoDB Workbook >> MongoDB Documents

Create a Collection

If a collection does not exist, MongoDB creates the collection when you first store data for that collection.

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-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
      }
    ]
   } 
 )