Difference between revisions of "MongoDB InsertData"

From mi-linux
Jump to navigationJump to search
Line 77: Line 77:
 
* 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
 
* Try and add Department 30
 
* Try and add Department 30
 +
 +
 +
== Employee data ==
 +
 +
More than one record can be added at a time. The following examples will create a collection called ''emp'' representing the emp table data.
 +
 +
db.emp.insert( [
 +
{
 +
empno: 7876,
 +
ename: "ADAMS",
 +
job: "CLERK",
 +
mgr: 7788,
 +
hiredate: "19-NOV-2015",
 +
sal: 1100,
 +
deptno: 20
 +
},
 +
{
 +
empno: 7499,
 +
ename: "ALLEN",
 +
job: "SALESMAN",
 +
mgr: 7698,
 +
hiredate: "20-FEB-1995",
 +
sal: 1600,
 +
comm: 300,
 +
deptno: 30
 +
},
 +
{
 +
empno: 7698,
 +
ename: "BLAKE",
 +
job: "MANAGER",
 +
mgr: 7839,
 +
hiredate: "01-MAY-1981",
 +
sal: 2850,
 +
deptno: 30
 +
},
 +
{
 +
empno: 7782,
 +
ename: "CLARK",
 +
job: "MANAGER",
 +
mgr: 7839,
 +
hiredate: "09-JUN-1989",
 +
sal: 2450,
 +
deptno: 10
 +
},
 +
{
 +
empno: 7902,
 +
ename: "FORD",
 +
job: "ANALYST",
 +
mgr: 7566,
 +
hiredate: "03-DEC-1991",
 +
sal: 3000,
 +
deptno: 20
 +
}
 +
])
 +
  
 
== Next Step ==
 
== Next Step ==
  
 
[[MongoDB_QueryData|Querying]] the collection
 
[[MongoDB_QueryData|Querying]] the collection

Revision as of 12:08, 11 November 2017

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 10

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.

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"

})

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
  • Try and add Department 30


Employee data

More than one record can be added at a time. The following examples will create a collection called emp representing the emp table data.

db.emp.insert( [
{

empno: 7876,

	ename: 	"ADAMS",
	job: 	"CLERK",
	mgr:	7788,
	hiredate: "19-NOV-2015",
	sal:	1100,
	deptno:	20

}, { empno: 7499,

	ename: 	"ALLEN",
	job: 	"SALESMAN",
	mgr:	7698,
	hiredate: "20-FEB-1995",
	sal:	1600,

comm: 300,

	deptno:	30

}, { empno: 7698,

	ename: 	"BLAKE",
	job: 	"MANAGER",
	mgr:	7839,
	hiredate: "01-MAY-1981",
	sal:	2850,
	deptno:	30

}, { empno: 7782,

	ename: 	"CLARK",
	job: 	"MANAGER",
	mgr:	7839,
	hiredate: "09-JUN-1989",
	sal:	2450,
	deptno:	10

}, { empno: 7902,

	ename: 	"FORD",
	job: 	"ANALYST",
	mgr:	7566,
	hiredate: "03-DEC-1991",
	sal:	3000,
	deptno:	20

} ])


Next Step

Querying the collection