Blogs

Get schema information in MongoDB

May 01, 2013 by akash sharma

In this blog I am going to share the steps for getting information of schema of a collection.

Let say we have a collection named “posts” and we want to get names of fiels in all documents in that collection.

> mr = db.runCommand({ "mapreduce" : "posts", "map" : function() { for (var key in this) { emit(key, null); } },"reduce" : function(key, stuff) { return null; } , "out": "posts" + "_keys"});

{

      "result" : "posts_keys",

      "timeMillis" : 441,

      "counts" : {

              "input" : 1000,

              "emit" : 8000,

              "reduce" : 80,

              "output" : 8

      },

      "ok" : 1

}

> db[mr.result].distinct("_id")

[

      "_id",

      "author",

      "body",

      "comments",

      "date",

      "permalink",

      "tags",

      "title"

]

> 

The output of second command gives names of all fields in “posts” collection.

 

Akash Sharma

Akash.sharma@oodlestechnologies.com

Basic queries for CRUD operations in MongoDB II

May 01, 2013 by akash sharma

In this blog I am going to share some more CRUD operations in mongoDB.

Previously I have written a blog for   Basic queries for CRUD operations in MongoDB .

Let say we have a database “school” having collection “student”.

 

Example 1: How to rename a field in a collection.

The $rename operator updates the name of a field. The new field name must differ from the existing field name.

I have a field named “class” of a student.I want to rename it as “standard”.

> db.student.update( {} , {$rename:{"class" : "standard"}} ,false , true)

The first blank curly braces indicates that you have to update all the documents.

parameters false and true are for upsert and multi options respectively.

 

Example 2: How to get a formatted output of all the documents.

If I apply the command

>db.student.find()

I will get an output like this:

> db.student.find()

{ "_id" : ObjectId("517ce315ebdc79a0c6ad6948"), "age" : 15, "name" : "rahul", "s

tandard" : 9, "subjects" : [ "hindi", "maths", "science" ] }

{ "_id" : ObjectId("517ce328ebdc79a0c6ad6949"), "age" : 17, "name" : "ashok", "s

tandard" : 10, "subjects" : [ "hindi", "maths", "science" ] }

{ "_id" : ObjectId("517ce341ebdc79a0c6ad694a"), "age" : 14, "name" : "ankur", "s

tandard" : 6, "subjects" : [ "hindi", "maths", "science" ] }

> 

 

To make a formated output we can apply this command:

> db.student.find().pretty()

{

       "_id" : ObjectId("517ce315ebdc79a0c6ad6948"),

       "age" : 15,

       "name" : "rahul",

       "standard" : 9,

       "subjects" : [

               "hindi",

               "maths",

               "science"

       ]

}

{

       "_id" : ObjectId("517ce328ebdc79a0c6ad6949"),

       "age" : 17,

       "name" : "ashok",

       "standard" : 10,

       "subjects" : [

               "hindi",

               "maths",

               "science"

       ]

}

{

       "_id" : ObjectId("517ce341ebdc79a0c6ad694a"),

       "age" : 14,

       "name" : "ankur",

       "standard" : 6,

       "subjects" : [

               "hindi",

               "maths",

               "science"

       ]

}

> 

 

 

Example 3: How to get output for limited number of fields but not for all.

Let say I want to see only name, age and standard for all documents:

> db.student.find({},{name:1,standard:1,age:1}).pretty()

{

       "_id" : ObjectId("517ce315ebdc79a0c6ad6948"),

       "age" : 15,

       "name" : "rahul",

       "standard" : 9

}

{

       "_id" : ObjectId("517ce328ebdc79a0c6ad6949"),

       "age" : 17,

       "name" : "ashok",

       "standard" : 10

}

{

       "_id" : ObjectId("517ce341ebdc79a0c6ad694a"),

       "age" : 14,

       "name" : "ankur",

       "standard" : 6

}

> db.student.find({},{name:1,standard:1,age:1,_id:0}).pretty()

{ "age" : 15, "name" : "rahul", "standard" : 9 }

{ "age" : 17, "name" : "ashok", "standard" : 10 }

{ "age" : 14, "name" : "ankur", "standard" : 6 }

> 

The first curly braces in find() will filter the documents on some condition.

The second curly braces determines which fields you want to show.

If you want to see the value for a particular field you have to set its value as 1.

By default value for _id field is 1 and 0 for all other fields.


 

Example 4: How to get output for limited number of fields but not for all for a given condition.

I want to see only name, age and standard for documents having standard greater than 8:

> db.student.find({standard:{$gt:8}},{name:1,standard:1,age:1,_id:0})

{ "age" : 15, "name" : "rahul", "standard" : 9 }

{ "age" : 17, "name" : "ashok", "standard" : 10 }

> 

 

 

Example 5 : How to remove a field from all documents of a collection.

suppose we want to remove age field from all documents:

> db.student.update({age:{$exists:true}},{$unset:{age:1}},false,true)

Here $exists checks in those documents which contains age field.

$unset removes the field.

false and true are for upsert and multi options respectively.

 

Akash Sharma

Akash.sharma@oodlestechnologies.com

Recovering from MongoDB crashes on ubuntu or windows

May 01, 2013 by akash sharma

Sometimes during development  with MongoDB I have experienced that MongoDB crashes  , In this blog I am sharing how to recover from a crashed MongoDB instance.

First I am going to show steps in windows OS:

There is a file named “mongod.lock” in C:\data\db directory.

First you need to delete this file.

Then you need to restart mongodb by applying following command in command prompt with administrative priviledges:

net start MongoDB

 

 

Now I am going to show steps in Ubuntu OS:

There is a file named “mongod.lock” in /var/lib/mongodb directory.

You need to delete this file by the following command:

sudo rm  -f  /var/lib/mongodb/mongod.lock

 

Then you need to restart mongodb by applying following command in terminal:

sudo service mongodb start

 

 

Akash Sharma

akash.sharma@oodlestechnologies.com

 

Scaling MongoDB and setting up sharding for MongoDB on Ubuntu

February 07, 2013 by akash sharma

In this blog I am going to share the details on how to implement sharding in mongodb on Ubuntu with data splitting into shards.

Before starting I want you to go through my blog:

FAQs : Scaling MongoDB using Sharding

In this blog I have mentioned all the terms related to sharding the mongodb database.

To start with sharding I just want to list the requirements :

  • shard server : minimum two servers for checking the splitting of data
  • config server : one server for testing or three servers for production
  • routing server : one server for mongos process

I have implemented it by myself as a testing work.I launched 2 shard servers , one config server and one routing server.All the servers were on EC2 ubuntu machine.Throughout the blog I will share hostname and private IP as the values which I have implemented.The machines with these IPs are terminated.You can implement your own machine instance.Keep in mind that following port numbers should be active on these machine:
22, 80, 8080, 27017, 27018, 27019, 28017, 28018, 28019

Following are the list of configurations of all the servers that I used:

2 Shard server:
machine name: Mongo-Machine-1
command to connect server :
ssh -i mongodbconnect.pem ubuntu@ec2-23-22-195-45.compute-1.amazonaws.com
hostname: ec2-23-22-195-45.compute-1.amazonaws.com
Private IP : 10.204.22.130

machine name: Mongo-Machine-2
command to connect server:
ssh -i mongodbconnect.pem ubuntu@ec2-75-101-188-189.compute-1.amazonaws.com
hostname: ec2-75-101-188-189.compute-1.amazonaws.com
Private IP: 10.207.25.190

1 Config server:
machine name: Mongo-Machine-3
command to connect server:
ssh -i mongodbconnect.pem ubuntu@ec2-23-20-67-5.compute-1.amazonaws.com
hostname: ec2-23-20-67-5.compute-1.amazonaws.com
private IP : 10.210.50.142
(IP of config server will be needed at the setting cluster)

1 Routing server:
machine name: Mongo-Machine-4
command to connect server:
ssh -i mongodbconnect.pem ubuntu@ec2-23-20-79-29.compute-1.amazonaws.com
hostname: ec2-23-20-79-29.compute-1.amazonaws.com

Cluster Setup:
Install mongodb on all servers.For installing mongodb on ubuntu go to my blog :
Installing MongoDB on Windows or Linux ( Ubuntu ) environment


Setting at all shard servers:

//Connect to shard server
ssh -i mongodbconnect.pem ubuntu@ec2-23-22-195-45.compute-1.amazonaws.com

//first stop the mongodb service
sudo service mongodb stop

//open mongodb.conf file at /etc/init and add --shardsvr parameter
sudo vim /etc/init/mongodb.conf

"exec start-stop-daemon --start --quiet --chuid mongodb --exec  /usr/bin/mongod -- --config /etc/mongodb.conf;"
//Replace the above line with the line below
"exec start-stop-daemon --start --quiet --chuid mongodb --exec  /usr/bin/mongod -- --shardsvr --config /etc/mongodb.conf;"

//Now start mongodb service
sudo service mongodb start


 

port no for shard server is 27018 on command shell and 28018 for RESTful service
To test that mongodb is running fine you can hit URL:
hostname:28018
eg: for machine name: Mongo-Machine-1
hostname: ec2-23-22-195-45.compute-1.amazonaws.com:28018
OR
Testing this in terminal:

	mongo localhost:27018



Setting at all config servers:

//Connect to config server
ssh -i mongodbconnect.pem ubuntu@ec2-23-20-67-5.compute-1.amazonaws.com

//first stop the mongodb service
sudo service mongodb stop

//open mongodb.conf file at /etc/init and add --configsvr parameter
sudo vim /etc/init/mongodb.conf

"exec start-stop-daemon --start --quiet --chuid mongodb --exec  /usr/bin/mongod -- --config /etc/mongodb.conf;"
// Replace the above line with the line below
"exec start-stop-daemon --start --quiet --chuid mongodb --exec  /usr/bin/mongod -- --configsvr --config /etc/mongodb.conf;"

//start mongodb service
sudo service mongodb start


port no for config server is 27019 on command shell and 28019 for RESTful service
To test that mongodb is running fine you can hit URL:
hostname:28019
eg:
ec2-23-20-67-5.compute-1.amazonaws.com:28019
OR
Testing this in terminal:

	mongo localhost:27019


Perform following steps on Routing server

//Connect to Routing server
ssh -i mongodbconnect.pem ubuntu@ec2-23-20-79-29.compute-1.amazonaws.com

//stop mongodb service
sudo service mongodb stop

//starting mongos process with configdb parameter to configure config server
//values for configdb parameter should be either IP or hostname
//for testing use one config server , for production use 3 config server
mongos --configdb 10.210.50.142

//you will get a message like this:
//”config servers and shards contacted successfully”

//open new tab with this IP (Routing server)

//Enter in admin database
mongo admin

//Adding shards to the routing server.
//shard can be added only through admin database
mongos>db.runCommand( { addshard : "10.204.22.130:27018", name : "Shard_A" } );
mongos>db.runCommand( { addshard : "10.207.25.190:27018", name : "Shard_B" } );

//enabling the database “testDBSharding” for sharding
mongos> db.runCommand( { enablesharding : "testDBSharding" } );
#output
{ "ok" : 1 }

//Mention the name of collection “testData” to be sharded with key name
mongos> db.runCommand( { shardcollection : "testDBSharding.testData", key : { _id : 1 } } );
#output
{ "collectionsharded" : "testDBSharding.testData", "ok" : 1 }

mongos> show dbs
#output
admin    (empty)
config    0.046875GB
testDBSharding    0.203125GB

mongos> use testDBSharding
#output
switched to db testDBSharding

//Script for entering large size data
mongos> myData = "";while ( myData.length < 200000 )myData += "My data for mongodb sharding";for ( var num = 0; num < 5000; num++ ){db.testData.save( { myData : myData } );}
mongos> use config
#output
switched to db config

//Showing all chunk information
mongos> db.chunks.find();

//Showing status of sharded cluster
mongos> db.printShardingStatus();
#Output
--- Sharding Status ---
 sharding version: { "_id" : 1, "version" : 3 }
 shards:
   {  "_id" : "Shard_A",  "host" : "10.204.22.130:27018" }
   {  "_id" : "Shard_B",  "host" : "10.207.25.190:27018" }
 databases:
   {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
   {  "_id" : "testDBSharding",  "partitioned" : true,  "primary" : "Shard_A" }
            testDBSharding.testData chunks:
                                    Shard_A    15
                                    Shard_B    12
                        too many chunks to print, use verbose if you want to force print

mongos>



 

Checking data at shard_A server

/*Note:Do not try to update or delete data from shard server
 in a sharded collection.This is only for conformation that
 data is saved on the shard.*/

//Connect to shard server
ssh -i mongodbconnect.pem ubuntu@ec2-23-22-195-45.compute-1.amazonaws.com

//use mongo with port no 27018
mongo localhost:27018/testDBSharding

>show collections

>db.testData.stats();

 

 

 

Akash Sharma

akash.sharma@oodlestechnologies.com

FAQs : Scaling MongoDB using Sharding

February 07, 2013 by akash sharma

In this blog I am going to share some conceptual aspects of Sharding in MongoDB. If you want to jump to implementation of sharding please follow next blog Scaling MongoDB and setting up sharding for MongoDB on Ubuntu

But before you should understand some basic facts about sharding.

Question: What is sharding?

Answer:
Sharding is splitting the data of a database collection on more than one server.MongoDB handles all the following complexities automatically :

  • Distributing data over all database servers
  • Balancing data load at all servers
  • Cope up if a DB server gets down
  • Fetching the data for a query from a server in an optimized way



Question: What is the logic behind splitting collection data over more than one server?
Answer:
For implementing sharding over a collection, you have to select a shard key. Shard key can be a field or set of fields in collection. Every server (shard) has a unique range in shard key value.
I can explain the logic with an example:
let us say you have a collection called userList.
You choose the shard key as username.
Lets assume that you have 3 servers for distributing collection data (Server1, Server2, Server3).

So initially you have all the data on single server(shard) i.e. Server1.
range for shard key for server1  is [ -∞ , ∞)
[ a, b )  means a =< x < b

As your data increases enormously, data starts dividing on multiple servers (Mongodb handles this dividing and load balancing automatically).
Now the shard key set divides itself into 3 subsets.For example it can be like this:
server 1 : [ -∞ , f )
server 2 : [ f , p )
server 3 : [ p , ∞ )

Shard key splits itself into ranges of shard key value.That is why this method is known as “Range Based Splitting”.
All subsets of a shard key set are mutually exclusive.

So now all user names starting with character value less than “f” are stored on server1.
While searching for the data it searches with a query “get document be username” and depending on the result it goes to the proper server.


Question: What are the important facts about shard key?
Answer:
A value in shard key cannot be null.
You cannot update the value of shard key.
Selecting a proper shard key is one of the most important aspect of sharding a collection.Shard key effects on two main aspects of clustering data:

  • balancing data over the servers
  • fetching data over queries on a collection

That is why schema design for database in MongoDB becomes very important.


Question: How MongoDB sorts data for a field?
Answer:
The way of sorting data in a field is:
null < numbers < strings < objects < arrays < binary data < ObjectIds < booleans < dates < regular expression


Question: What is a Shard in MongoDB?
Answer:
A shard is one or more servers in a cluster that are responsible for some subset of the data.For example if we had a cluster that have 5,000,000 documents than one shard might contain 1,000,000 documents.


Question: What are chunks in MongoDB?
Answer:
Each Shard is divided into one more chunks of data.Following points will elaborate information about chunks:

  • If data increases on a shard it automatically creates a new chunk and starts filling it up.
  • If the difference between number of chunks of two shard increases a limit, it automatically transfers some chunks to the shard having less chunks to make a balance of data between two shards.
  • Usually the size of a chunk is about 64 MB. If difference in number of chunks between two shards is 9 or more than chunks start transferring from one shard to another.(Note: These values are just estimates. I am not 100% sure about this as mongodb handles all these complexities automatically.)
  • If you add a new shard to the cluster than some chunks from all the previously existing shards are transferred to the new chunk to make a balance in cluster.
  • The reason of choosing chunk size as 64 MB is that, this value is neither too small nor too large.If chunk size become smaller than it will reduce performance by transferring chunks all the time.If chunk size becomes larger than all data will start piling up on single shard and make the data disbalanced.

NOTE: In mongodb you can set the size of a chunk to a value as your wish.But be sure that it can effect on balancing the load of data. So try not to do it.


Question: What is mongos ?
Answer:
mongos is the interaction point between users and the cluster.Its job is to have a single server interface with the user.
When you use a cluster, you connect to mongos and issue all reads and writes to that mongos.You should never access the shards directly (although you can if you want).
mongos forwards all user requests to the appropriate shards. If a user inserts a document,
mongos looks at the document’s shard key, looks at the chunks, and sends the
document to the shard holding the correct chunk.
For example, say we insert {"foo" : "bar"} and we’re sharding on foo. mongos looks
at the chunks available and sees that there is a chunk with the range ["a”, “c”), which
is the chunk that should contain “bar”.
 





Question: What is difference between shard server and config server?
Answer:
Shard server is used to store the actual data.
Config server are special mongod processes that are used to store information about cluster.It stores all the information related to shards , mongos process and system administrator.
For a successful migration of data to shard server all config servers must be up.If any config server is down, while chunk migration from one shard to another, whole process is revert back.


Anatomy of Cluster
A MongoDB cluster basically consists of three types of processes: the shards for actually
storing data, the mongos processes for routing requests to the correct data, and the
config servers, for keeping track of the cluster’s state.

 

Each of the components above is not “a machine.” mongos processes are usually run
on appservers. Config servers, for all their importance, are pretty lightweight and can
basically be run on any machine available. Each shard usually consists of multiple machines,as that’s where the data actually lives.

 

 

Akash Sharma

akash.sharma@oodlestechnologies.com

Basic queries for CRUD operations in MongoDB

January 30, 2013 by akash sharma

In this blog I am going to share some basic quiries for mongoDB.

Creating a empty database and adding an authenticated User to that database:

mongo

>use mydb

//opens database with given name

//and sets this database as current DB.

//if this DB does not exit it will create an empty DB.

>db.addUser("userName", "password");

//this user can only access the current database i.e. mydb 

//Now exit from the mongoDB:

>exit;

Now enter the mongoDB as authenticated user:

mongo -u username -p password 127.0.0.1/myDB       
           
//it opens DB at given IP with username and password

> j = { name : "sachin" , salary : 1000 , age : 20};

> db.myCollection.save(j);

//creates a collection myCollection and insert a document in it.
//this collection is inserted in current DB.

 

Note:
collection in mongoDB is same as table in mysql.
document in mongoDB is same as row in mysql.
When you apply save method to a collection for the first time a new collection of that name is created and document is inserted into it.

In MongoDB there is no predefinition of fields (what would be columns in an RDBMS). There is no schema for fields within documents – the fields and their value datatypes can vary.
MongoDB collections are essentially named groupings of documents. You can think of them as roughly equivalent to relational database tables.
MongoDB is a schema-free (or more accurately, "dynamic schema") database.

Upon being inserted into the database, objects are assigned an object ID (if they do not already have one) in the field _id.

 

Saving new entry in "myCollection"  collection

 

> db.myCollection.save({"name" : "rahul", "salary" : 2000, "age" : 22 });

> db.myCollection.save({"name" : "saurabh", "salary" : 3000, "age" : 23, "mobile" : 911888812});

> db.myCollection.save({"age" : 20, "salary" : 500, "name" : "virender"});

> db.myCollection.save({"age" : 20, "salary" : 600, "name" : "gautam"});

 

Document Object Id:

ObjectId: A special 12-byte BSON type that has a high probability an ObjectId represent the time of the ObjectID’s creation. MongoDB uses ObjectId values as the default values for _id fields.

_id : A field containing a unique ID, typically a BSON ObjectId. If not specified, this value is automatically assigned upon the creation of a new document. You can think of the _id as the document’s primary key.
 

Retrieve data from “myCollection” collection

You can retrieve documents from MongoDB using either of the following methods:

  • find()
  • findOne()
     

The find() method has the following syntax:

db.collection.find( <query>, <projection> )

·        If there is no <query> argument, the find() method selects all documents from a collection.

·        If there is a <query> argument, the find() method selects all documents from a collection that satisfies the criteria of the query.

 

>db.myCollection.find();

//prints all documents of a collection

 

Example 1: The following operation returns all documents in “myCollection” having name=sachin .
 

> db.myCollection.find( {name:’sachin’} );

 

Comparison Operator

$all

$gt

$gte

$in

$lt

$lte

$ne

 

Logical Operator

$and

$nor

$not

$or

 

Example 2: find all entries where name starts with "r" and age =22.

>db.myCollection.find( { name:/^r/,age:22 } );

 

Example 3: find all entries where name=rahul or name=sachin.

>db.myCollection.find({name : {$in: ['rahul','sachin']}});

 

Example 4: find all entries where name contains character "s" or salary >=22.

>db.myCollection.find( { $or :[ { name : /.*s.*/ } , { age : { $gte : 22 } } ] } );

//For multiple AND criteria on the same field, use the $and operator.

 

Example 5: find name and age of all entries where name has a charater 'b'.

>db.myCollection.find({ name:/.*b.*/ } , { name:1, age:1,  _id : 0 });

 

Example 6: find all names in collection in sorted order by name in ascending order.

>db.myCollection.find( {} , {name:1, _id:0} ).sort({name:1});

 

Note:

The _id field is implicitly included in the <projection> argument. In projections that explicitly include fields, _id is the only field that you can explicitly exclude. Otherwise, you cannot mix include field and exclude field specifications.

 

Example 7: find all names in collection in sorted order by age in ascending order and if age is same sort it by name in descending order.

>db.myCollection.find( {} , { name:1,age:1, _id:0 } ).sort( {age :1, name :-1} );

 

Example 8: find all names having salary greater than 500 in collection in sorted order by age in ascending order and if age is same sort it by name in descending order.

>db.myCollection.find(  { salary : {$gt:500} } , { name : 1 , age : 1, _id : 0 }).sort( {age:1, name:-1} );

 

For more help:

http://docs.mongodb.org/manual/reference/operators/

 

How to Update a document :

The update() method modifies an existing document or documents in a collection. By default the update() method updates a single document. To update all documents in the collection that match the update query criteria, specify the multi option. To insert a document if no document matches the update query criteria, specify the upsert option.

 

db.collection.update(query, update, <upsert,> <multi>)

The update() method takes the following parameters:

Parameters:

query (document) – Specifies the selection criteria for the update. The query parameter employs the same query selectors as used in the db.collection.find() method.

update (document) – Specifies the modifications to apply.

If the update parameter contains any update operators expressions such as the $set operator expression, then:

·         the update parameter must contain only update operators expressions.

·         the update method updates only the corresponding fields in the document.

If the update parameter consists only of field: value expressions, then:

·         the update method replaces the document with the updates document. If the updates document is missing the _id field, mongoDB will add the _id field and assign to it a unique objectId .

·         the update method updates cannot update multiple documents.

options : (optional) Specifies whether to perform an upsert and/or a multiple update. Use the options parameter instead of the individual upsert and multi parameters.

upsert : A kind of update that either updates the first document matched in the provided query selector or, if no document matches, inserts a new document having the fields implied by the query selector and the update operation. The default value is false. When true, the update() method will update an existing document that matches the query selection criteria or if no document matches the criteria, insert a new document with the fields and values of the update parameter and if the update included only update operators, the query parameter as well .

multi : (optional) Specifies whether to update multiple documents that meet the query criteria.

When not specified, the default value is false and the update() method updates a single document that meet the query criteria.

When true, the update() method updates all documents that meet the query criteria.

 

Example9 : change salary = 5000 in collection where name == ‘sachin’ .

>db.myCollection.update(  {name : 'sachin'} , { $set : {salary : 5000}}  );

//This will update the first document having name=sachin. (multi is set to false by default, upsert is set to false by default)

 

Example 10: change salary=200 for all entries where age=20.

>db.myCollection.update({age:20}, {$set:{salary:200}}, false, true);

 

Example 11: Add new field “status” having value as “senior” if salary is greater than or equal to 1000.

>db.myCollection.update( {salary : { $gte: 1000 } } , { $set: { 'status' : 'senior' } } , false , true);

 

Example 12: Add new field  “profile” to all documents as “player”.

>db.myCollection.update( {}, { $set :{ 'profile': 'player' } } , false , true );

 

Example 13: Drop field “status” from all documents.

> db.myCollection.update( {status: {$exists: true}}, {$unset: {status: 1}}, false, true);

 

for more info on update go to:

http://docs.mongodb.org/manual/reference/method/db.collection.update/



Get number of documents in a collection:

db.myTable.count();

 

How to delete documents from a collection:

>db.mytable.remove({});

// removes all

>db.mytable.remove({ salary : 1000 });

// removes all where salary == 1000 

 

How to drop entire collection:

>db.mytable.drop();

 

How to delete database: 

//set the database which you want to delete as current database

>use mydb

>db.dropDatabase();

 

How to show all databases:

>show dbs

 

How to show all collections in current database:

>show collections

 

Showing statistics of collection:

>db.myCollection.stats();

 

Showing statistics of database:

>db.stats();

 

Here are some screenshots for the above commands:

  • Adding new DB and authenticated user

 

  • Saving new documents and retrieving data

 

 

  • Example2

 

 

  • Example3

 

  • Example4

 

 

  • Example5

 

 

  • Example6

 

 

  • Example7

 

 

  • Example8

 

  • Example9

 

  • Example10

 

  • Example11

 

  • Example12

 

 

  • Example13

 

For some advanced mongoDB quiries I have written a blog Basic queries for CRUD operations in MongoDB II.

 

Akash Sharma

akash.sharma@oodlestechnologies.com

 

Installing MongoDB on Windows or Linux ( Ubuntu ) environment

January 29, 2013 by akash sharma

Brief Introduction of MongoDB

MongoDB (from "humongous" means enormous) is an open source document-oriented database system developed and supported by 10gen. Instead of storing data in tables as is done in a "classical" relational database, MongoDB stores structured data as JSON-like documents with dynamic schemas (MongoDB calls the format BSON), making the integration of data in certain types of applications easier and faster.
10gen began Development of MongoDB in October 2007. The database is used by MTV Networks, CraigsList, FourSquare and UIDAI Aadhaar. MongoDB is the most popular NoSQL database management system

Question : What is document oriented database?
Answer:

It is a computer designed for storing, retrieving, and managing document-oriented information.The central concept of a document-oriented database is the notion of a Document.
While each document-oriented database implementation differs on the details of this definition, in general, they all assume documents encapsulate and encode data (or information) in some standard formats or encodings. Encodings in use include XML, JSON, and  BSON.
Unlike a relational database where each record would have the same set of fields and unused fields might be kept empty, there are no empty 'fields' in either document (record) in this case.


Question : What is NoSQL database?
Answer:

NoSQL (commonly interpreted as "not only SQL”) , NoSQL databases are not built primarily on tables, and generally do not use SQL for data manipulation.
It is highly optimized for retrieval and appending operations when working with a huge quantity of data.
The data can be structured, but NoSQL is used when what really matters is the ability to store and retrieve great quantities of data, not the relationships between the elements.Usage examples might be to store millions of key-value pair in one or a few associative arrays or to store millions of data records.

  • Does not use SQL as its query language.
  • Developed to manage large volumes of data that do not necessarily follow a fixed schema.
  • NoSQL has a distributed, fault-tolerant architecture. In this way, the system can easily scale out by adding more servers, and failure of a server can be tolerated. This type of database typically scales horizontally.

Question:What is a document and a collection in mongodb?

Answer:

An element of data is called a document, and documents are stored in collections. One collection may have any number of documents.Compared to relational databases, we could say collections are like tables, and documents are like records. But there is one big difference: every record in a table has the same fields (with, usually, differing values) in the same order, while each document in a collection can have completely different fields from the other documents. The only schema requirement mongo places on documents (aside from size limits) is that they must contain an '_id' field with a unique, non-array value.

Note: "_id" field is obligatory, automatically created by MongoDB; it's a unique index which identifies the document. The user may specify any non-array value for _id as long as the value is unique).

Question : What is JSON like document ?
Answer :

MongoDB stores structured data as JSON-like documents, using dynamic schemas (called BSON), rather than predefined schemas.
In a document, new fields can be added or existing ones suppressed, modified or renamed at any moment. There is no predefined schema. A document structure is very simple: it follows the JSON format, and consists of a series of key-value pairs.The key of the key-value pair is the name of the field, the value in the key-value pair is the field's content. The key and value are separated by ":" .

 

Question: What is BSON ?

Answer:

A serialization format used to store documents and make remote procedure calls in MongoDB. “BSON” is a portmanteau of the words “binary” and “JSON”. Think of BSON as a binary representation of JSON (JavaScript Object Notation) documents. For a detailed spec, see bsonspec.org.

for more info:

http://docs.mongodb.org/manual/reference/glossary/#term-bson

 

Question:Advantages and disadvantages of Mongodb over classical relational database?

Answer:

  • highly optimized for retrieve and append operations.
  • manage large volumes of data that do not necessarily follow a fixed schema.
  • MongoDB can run over multiple servers, balancing the load and/or duplicating data to keep the system up and running in case of hardware failure. Automatic configuration is easy to deploy and new machines can be added to a running database.
  • This is a rich data structure capable of holding arrays and other documents. This means you can often represent in a single entity a construct what would require several tables to properly represent in a relational db.
  •  Deep query-ability: MongoDB supports dynamic queries on documents using a document-based query language that's nearly as powerful as SQL.
  • In some cases a non-relational database is not better than a relational one. If your database have a lot of relations and normalization, it might makes little sense to use something like mongodb. It's all about finding the right tool for the job.
  • MongoDB is fast but not ACID, it has no transactions.

 


Mongodb setup in windows:

Download MongoDB for Windows
Download the latest production release of MongoDB from http://www.mongodb.org/downloads .
Note : Always download the correct version of MongoDB for your Windows system. The 64-bit versions of MongoDB will not work with 32-bit Windows.
Extract the archive to C:\ by right clicking on the archive and selecting Extract All and browsing to C:\ .
The folder name will be either:
C:\mongodb-win32-i386-[version]
or
C:\mongodb-win32-x86_64-[version]
In both examples, replace [version] with the version of MongoDB downloaded.


Set up the Environment
Open a command prompt with “Administrative Privileges” and issue the following command
cd \
move C:\mongodb-win32-* C:\mongodb
MongoDB requires a data folder to store its files.
Issue the following command sequence:
md data
md data\db


Start MongoDB
To start MongoDB, execute from the Command Prompt:
C:\mongodb\bin\mongod.exe
This will start the main MongoDB database process.
Connect to MongoDB using the mongo.exe shell. Open another Command Prompt and issue the following command:
C:\mongodb\bin\mongo.exe
The mongo.exe shell will connect to mongod.exe running on the localhost interface and port 27017 by default.
At the mongo.exe prompt, issue the following two commands to insert a record in the test collection of the default test database and then retrieve that record:
> db.test.save( { a: 1 } )
> db.test.find()


MongoDB as a Windows Service
Setup MongoDB as a Windows Service, so that the database will start automatically following each reboot cycle.
You should specify two options when running MongoDB as a Windows Service: a path for the log output (i.e. logpath) and a configuration file.
Create a specific directory for MongoDB log files:
md C:\mongodb\log
Create a configuration file for the logpath option for MongoDB in the Command Prompt by issuing this command:
echo logpath=C:\mongodb\log\mongo.log > C:\mongodb\mongod.cfg


Install and Run the MongoDB Service
Run all of the following commands in Command Prompt with “Administrative Privileges:”

To install the MongoDB service:
C:\mongodb\bin\mongod.exe --journal --config C:\mongodb\mongod.cfg --install
Modify the path to the mongod.cfg file as needed. For the --install option to succeed, you must specify a logpath setting or the --logpath run-time option.


--journal will enable journal option.For more info go to http://docs.mongodb.org/manual/administration/journaling/

 

How to start or stop mongodb server on windows:
net start MongoDB
net stop MongoDB


Mongodb setup on ubuntu:


(1)sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10

(2)Create a /etc/apt/sources.list.d/10gen.list file
sudo vi /etc/apt/sources.list.d/10gen.list
and add the following line in this file.
deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen

(3)sudo apt-get update

(4)sudo apt-get install mongodb-10gen

How to start or stop mongodb server on ubuntu:
sudo service mongodb start
sudo service mongodb stop


 

In case mongodb service get crashed in windows:
If your mongodb service get crashed, start mongoDB with the
"mongod --repair"
flag, but after that you need to restart it.

For more information go to following link :

 http://docs.mongodb.org/manual/tutorial/recover-data-following-unexpected-shutdown/

 

In case mongodb service get crashed in ubuntu:

If your mongodb service get crashed , remove file "mongod.lock".

File location could be /srv/db/mongodb/mongod.lock

Then restart MongoDB service
/etc/init.d/mongodb start

 

 

Akash Sharma

akash.sharma@oodlestechnologies.com

http://oodlestechnologies.com/

 

Powered byApache Solr

Follow Us

Recent Entries