Here we’re inserting some data/record/documents into the collection person.
Database’s Before running the script
1
2
3
4
5
6
7
8
9
10
11
12
13
C:\>cd mongodb
C:\mongodb>cd bin
C:\mongodb\bin>mongo
MongoDB shell version: 2.4.3
connecting to: test
> show dbs
admin 0.203125GB
company 0.203125GB
local 0.078125GB
> exit
bye
C:\>cd mongodb
C:\mongodb>cd bin
C:\mongodb\bin>mongo
MongoDB shell version: 2.4.3
connecting to: test
> show dbs
admin 0.203125GB
company 0.203125GB
local 0.078125GB
> exit
bye
Before running the script, we have only 3 databases.
Running the script
1
2
3
C:\mongodb\bin>mongo 127.0.0.1/satish C:/temp/load.js
MongoDB shell version: 2.4.3
connecting to: 127.0.0.1/satish
C:\mongodb\bin>mongo 127.0.0.1/satish C:/temp/load.js
MongoDB shell version: 2.4.3
connecting to: 127.0.0.1/satish
Here, mongo is the JavaScript shall. 127.0.0.1 is nothing but our localhost. satish is the new database we are creating. C:/temp/load.js is the path of load.js file. We’re loading the contents of load.js file into new database satish.
Database’s After running the script
1
2
3
4
5
6
7
8
9
10
11
12
13
C:\mongodb\bin>mongo
MongoDB shell version: 2.4.3
connecting to: test
> show dbs
admin 0.203125GB
company 0.203125GB
local 0.078125GB
satish 0.203125GB
> use satish
switched to db satish
> show collections
person
system.indexes
C:\mongodb\bin>mongo
MongoDB shell version: 2.4.3
connecting to: test
> show dbs
admin 0.203125GB
company 0.203125GB
local 0.078125GB
satish 0.203125GB
> use satish
switched to db satish
> show collections
person
system.indexes
New database satish has been added and it has person collection, which we loaded from the JavaScript file.
Note: This method will be handy while migrating our application from one mongoDB server to another mongoDB server. There is import/export options in mongoDB, but this method is also helpful if we have some custom data to be inserted. nontheless a useful tool to have.
1 or true means, those {key: value} pairs need to be returned. 0 or false means, excluding those {key: value} pairs, all other {key: value} pairs(records) needs to be returned.
> db.info.find({name: 'Apple'}, {product: true, emp_no: false}).forEach(printjson);
Wed May 08 12:11:30.184 JavaScript execution failed: error: {
"$err" : "You cannot currently mix including and excluding fields.
Contact us if this is an issue.",
"code" : 10053
} at src/mongo/shell/query.js:L128
> db.info.find({name: 'Apple'}, {product: 0, emp_no: 1}).forEach(printjson);
Wed May 08 12:11:40.840 JavaScript execution failed: error: {
"$err" : "You cannot currently mix including and excluding fields.
Contact us if this is an issue.",
"code" : 10053
} at src/mongo/shell/query.js:L128
> db.info.find({name: 'Apple'}, {product: true, emp_no: false}).forEach(printjson);
Wed May 08 12:11:30.184 JavaScript execution failed: error: { "$err" : "You cannot currently mix including and excluding fields. Contact us if this is an issue.", "code" : 10053
} at src/mongo/shell/query.js:L128
> db.info.find({name: 'Apple'}, {product: 0, emp_no: 1}).forEach(printjson);
Wed May 08 12:11:40.840 JavaScript execution failed: error: { "$err" : "You cannot currently mix including and excluding fields. Contact us if this is an issue.", "code" : 10053
} at src/mongo/shell/query.js:L128
We can not combine true(1) and false(0) together in the second parameter.
Special Provision! But we can do it with _id(ObjectId)
Operation of second document/record in ‘info’ collection. This illustrates that the syntax for sub-objects, array and the normal key/value pair is same.
If we use findOne() method on this collection, and the condition being the name key, then the oldest document will be returned. Only one key: value pair is returned, as findOne() returns only single object.
In this video tutorial, we’ll look at the basic differences between MongoDB and traditional Database Management Systems. Also, commands to create database, collection, documents – insertion.
Topic of discussion: Key differences. Keyword differences. Schema-free collection. Database creation. Creation of Collection. Insertion of Documents. JavaScript Console/Shall.
Some Commands
Show Database Already Present
1
show dbs
show dbs
Create Database
1
use company
use company
In MongoDB, it’ll never actually create any database or collection until we start storing documents in it!
Point object ‘db’ to our new database, by switching to company database
1
2
3
4
>use company
Switched to db company
>db
company
>use company
Switched to db company
>db
company
Naming our Collection
1
db.info
db.info
So our collection name is info
Number of documents present
1
db.info.count()
db.info.count()
using count() method, we check the no of documents present inside info collection.
We create a JSON object data. We start storing { key: value } pair into it. emp is an array, which contains name of 2 employees. videos is a sub object.
Now using save method we insert this data object into info collection.
Save() Save() is a higher lever method. It checks to see if the object we’re inserting is already present in our collection, by checking the _id value we’re inserting. _id field(which is a primary key in MongoDB document) – If there is no matching _id field in a document, it’ll call insert method and insert the data. And if the data being entered has no _id value, insert() method will create _id value for us, and insert the data into the collection. – If _id matches to any document in our collection, it’ll use update() instead to update the previously present data with the new data entered by the user.
Note: There is no Schema for our collection. i.e., no column name and datatype. Since documents are in BSON format, insertion and finding data is much faster.
Schema-free design is flexible and is of much use in modern day web application.
Example: Users upload photos and tag names of people in the pic. This can be stored as an array of tags in a key value pair, for only those pics which has tags. For other pics, we need not create this array itself. This is flexible. Also BSON format helps create application with high Scalability.
< ?php
include_once('db.php');
$res = mysql_query("INSERT INTO apple VALUES('','Oracle')")
or die("sorry, database is busy!");
echo "Successful insertion";
?>
< ?php include_once('db.php'); $res = mysql_query("INSERT INTO apple VALUES('','Oracle')") or die("sorry, database is busy!"); echo "Successful insertion";
?>
if the insertion operation fails for some reason, the die function is invoked and the message “Sorry, database is busy!” is shown to the use and the script execution halts, and the “Successful insertion” message won’t be echoed.
Data in the database, as shown in the video: 1. Google 2. Apple 3. Microsoft 4. Oracle