“ MongoDB
is an open source, document oriented NoSQL database where data is
represented & stored in the form of
BSON(binary JSON) ”
Following is a key value representation of data in JSON format and its corresponding BSON.It adds some "extra" information to documents, like length of string and sub-objects. This makes traversal faster.
// JSON file
{ "hello" : "world" }
// BSON file
"\x16\x00\x00\x00\x02hello\x00
\x06\x00\x00\x00world\x00\x00"
Following is a key value representation of data in JSON format and its corresponding BSON.It adds some "extra" information to documents, like length of string and sub-objects. This makes traversal faster.
// JSON file
{ "hello" : "world" }
// BSON file
"\x16\x00\x00\x00\x02hello\x00
\x06\x00\x00\x00world\x00\x00"
\x16\x00\x00\x00 // total document size
\x02 // 0x02 = type String
hello\x00 // field name
\x06\x00\x00\x00world\x00 // field value
\x00 // 0x00 = type end of object
With
the growing use of Social Media platforms, large chunks of
unstructured data was created .Thus, a need to to persist this
unstructured data in the most efficient way possible hence NoSQL
databases came into existence.
Following
is a brief comparison between them,
SQL
|
No-SQL
|
|
|
When
between faster I/O and disk-space the choice is faster computation
time, irrespective of the fact that we are duplicating the data then
we must go for MongoDB
RDBMS
|
Mongo
|
|
|
Installation
on Ubuntu
Inorder
to install mongoDB on ubuntu, make sure you have a stable
version(here 16.4) of Ubuntu & root privileges to the current
ubuntu user & Follow the steps
-
On the terminal screen, run following command to import MongoDB keys to your server.mCreate a MongoDB list file in /etc/apt/sources.list.d/ with this command,
-
echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
-
Update the repository with apt commandsudo apt-get update
-
Install MongoDBsudo apt-get install -y mongodb-org
-
Create file ‘mongodb.service’sudo nano /lib/systemd/system/mongodb.service
and
copy the file contents as
follows
[Unit]
Description=MongoDB
Database Service
Wants=network.target
After=network.target
[Service]
ExecStart=/usr/bin/mongod
--config /etc/mongod.conf
ExecReload=/bin/kill
-HUP $MAINPID
Restart=always
User=mongodb
Group=mongodb
StandardOutput=syslog
StandardError=syslog
[Install]
WantedBy=multi-user.target
Save
the file and exit.
-
Now update the systemd service with command below,systemctl daemon-reload
create
db folder at /data
cd
/
sudo mkdir
data
cd
/data
sudo mkdir
db
To start mongoDB service
To start mongoDB service
sudo service mongod start
Start
service at boot time
systemctl
start mongod
systemctl
enable mongod
-
Start MongoDB without access control.mongod --port 27017 --dbpath /data/db
on
another terminal, Connect to the instance.
mongo
--port 27017
Basic
Operations
-
To create database
>
use testdb
switched
to db testdb
-
To list all databases
>
show dbs
testdb
0.000GB
-
To create a collection
>
db.createCollection(“company”)
{
"ok" : 1 }
-
To list all collections
>
show collections
company
company
-
To insert a document
>
db.company.insert({empName:'aaa',empNo:101})
WriteResult({
"nInserted" : 1 })
-
To display the document
>
db.company.find();
{
"_id" : ObjectId("5923e409848dfe0c013c5977"),
"empName" : "aaa", "empNo" : 101 }
>
db.company.find().pretty();
{
"_id"
: ObjectId("5923e409848dfe0c013c5977"),
"empName"
: "aaa",
"empNo"
: 101
}
>
Comments
Post a Comment