Thursday, 19 February 2015

mongodb : Drop database

“db.dropDatabase()” method is used to drop database.

> show dbs
admin   (empty)
local   0.078GB
sample  0.078GB
test    (empty)
>
>
>
>
> show dbs;
admin   (empty)
local   0.078GB
sample  0.078GB
test    (empty)
>
>
> use sample;
switched to db sample
>
>
> db.dropDatabase();
{ "dropped" : "sample", "ok" : 1 }
>
> show dbs;
admin  (empty)
local  0.078GB
test   (empty)
>

Prevoius                                                 Next                                                 Home

Wednesday, 18 February 2015

mongodb : Create database

Connect to a database using 'use {database_name}' command. Even the database doesn't exist, you can connect to it using this command. Database is created on the fly once you start inserting data into it.

 

> use sample
switched to db sample
>
> show dbs
admin  (empty)
local  0.078GB
test   (empty)
>
> db.employee.insert({"id":1,"firstName":"Hari Krishna","lastName":"Gurram"})
WriteResult({ "nInserted" : 1 })
>
> show dbs
admin   (empty)
local   0.078GB
sample  0.078GB
test    (empty)

Above snippet creates database “sample”.

“show dbs” 
“show dbs” command displays all databases in the system. To display a database, it should contain at least one document. So when I call first time, it don’t display database “sample”.

db.employee.insert({"id":1,"firstName":"Hari Krishna", "lastName":"Gurram"}) 
Above statement insert a document into employee collection.

Note: 
 “test” is the default database in mongoDB. If you don’t create any database, then all the collections are stored into database “test”.


 
Prevoius                                                 Next                                                 Home

BSON

BSON stands for Binary script object notation. MongoDB represents data in BSON format. BSON format used by mongoDB follows the specification provided by http://bsonspec.org/ (Have a look at this site for detailed view like what data types BSON supports etc.,).

 

Down the line, when you interact with MongoDB using json, MongoDB drivers convert the JSON content into BSON format and store.




Why BSON?

a.   BSON is efficient than JSON while storing and retrieving the information

b.   Consume less space than json

c.    BSON support additional data types. For example, ObjectID is not a valid data type in JSON. When you insert a document into MongoDB, it automatically assigns a unique id to it. The type of this id is ObjectId.


 
Prevoius                                                 Next                                                 Home

CRUD operations

CRUD stands for create, read, update and delete. Below table shows the semantic relation between the terms used in RDBMS and mongoDB. For example, Delete operation is treated as remove in mongoDB and and delete in RDBMS. 

Operation
MongoDB
RDBMS
Create
Read
Update
Delete


Prevoius                                                 Next                                                 Home