Tuesday, 7 June 2016

Inserting a filed:

Inserting a field is pretty easier:
db.collectionname.update(
               {},
               {$set:{key:value},false,true}
               )

Upsert: If set to true, creates a new document when no document matches the query criteria.
Multi: If set to true, updates multiple documents that meet the query criteria. If set to false, updates one document.
db.collectionname.update(
               {},
               {$set:{key:value},{upsert:false,multi:true}}
               )

Monday, 18 January 2016

MongoDB -- Replication

Replication is making a duplicate copy of data. in mongo db duplication of the data is possible. Replication is made because to make the data available 24x7 even when there is a network issue, when there is a  system failure.
the process of the replication is such that the copy of data is saved in another server along with the main server. when ever the main server crashes, alternate server will comes into the picture.

this replication process helps in the hardware and service failures. Replication is done by replica set. 

MongoDB -- Aggregation

the aggregation operation here collects the values form the collection and perform the methods that are predefined in mongodb:
we use aggregate(). the below are some of the aggregate functions:
$sum: sums up the  value of the documents in the collection.

$min:gets the minimum value from the values of the document in collection.

$max:gets the maximum value from the values of the document in collection.

$first:gets the first record from the documents according to the grouping.

$avg: to calculate the average of all the values in the document.

$push:inserts a value in the array of the document.

$addToSet: insert the value in the array of the document but does not insert the duplicate documents.

$last:gets the last record from the document according to the grouping.

some of the key commands that makes the query more efficient by binding the aggregate functions to them. the following are the list:

$project : only specific fields can be selected

$match:

$group: the actual aggregation function for grouping.

$sort: sorting of the document

$skip:with in the available documents we can go to the list of the documents or needed documents.

$limit:this is the limitation on the documents that how many to be searched for.

$unwind: when an embeded document is present in the collection, it makes the relation as a join. so in order to use it as a independent document we have to use $unwind.





Wednesday, 6 January 2016

MongoDB -- Drop

To drop a data base, you should be in that database, because the current  database which you are working has to be deleted, here's a few examples for you:

Create a database

  command: use exdb

Check your current database

 command: db
Drop the database:
 command: db.dropDatabase()

output: {"ok":1}

    

MongoDB -- Indexes

searching the data based on some query becomes very pathetic. so we use indexes. index is just same as indexes that are seen in the textbooks. through the indexes we go directly to the pages rather than searching the entire book. in books the indexes holds page numbers of  contents of book. in a same manner, indexes here holds some specific value so that traversing to the specific data field is possible.

 creating an index

command:
  db.collectionname.ensureIndex({"key1":1}) --- ascending order index
  db.collectionname.ensureIndex({"key1":-1}) --- descending order index

or 
  db.collectionname.ensureIndex({"key1":1,"key2":-1})

MongoDB -- sorting

Sorting:

sording in ascending and descending order for documents in collection is possible. sort() method is used for this. it takes one parameter:
1 -- ascending
-1 -- descending

command:
db.collectionname.find().sort({"key":1})
db.collectionname.find().sort({"key":-1})

the default is ascending order

MongoDB -- Update

Updating the collection:

db.collectionname.update({key1:value1},{$set:{key2:value2}},{multi:true})


similarly equal to update collectionname set key2 = value2 where key1 = value1

MongoDB -- Collections

Creation of Collections:

we use tables in the traditional databases, here we use collections, the columns are called as fields, and joins are embedded documents.

lets create a simple collection:

first create a database: use dataset

then check the db: db

create a simple collection:
command: db.createCollection("customers")
 output : {ok:1}

we call record as documents here:
now insert a document in collection "dataset":

command:db.customers.insert({'name':'raj'})

to see the columns or data in tables we use select statement , here we use find() method.

command:db.customers.find()
output: ("_id":ObjectId(568cbca55685654adasda),"name":"raj")
 here the id for each field is created automatically, it is index of the field.


if you want the structure formatted of the document: use the following command:
command: db.customers.find().pretty()



Tuesday, 5 January 2016

MongoDB -- Basic Commands

Basic Commands:

the following are some of the basic commands:
  

ctrl+l or cls 

    --  clears your screen.

show dbs

    -- displays all the database names.

show collections

   -- displays the collection names

use <databasename> 

   -- creates the database with a name

help

   -- help command for basic help for commands

db.help()

 -- also a help command with all the methods specified by mongodb

db

-- prints the current database name.

MongoDB -- limit() and skip()

Limitation of documents:

we can query on the basis of how much of limit of documents and also skip them:

following are the commands:
    db.collection_name.find({key:value}).limit(2)
    db.collection_name.find({key:value}).limit(2).skip(2)




MongoDB -- Projection(select)

Projection:


Selecting of necessary data is called Projection:
the following command makes a projection:

db.collection_name.find()
          -- similarly equal to "select * from collection_name".
db.collection_name.find({"key":"value"})
          -- similarly equal to "select * from collection_name where key=value"

MongoDB -- BackUp

creating a back up:
before creating a back up make sure that you have the following folders that are created and dbpath is set:

to set the dbpath: >mongod.exe --dbpath "H:\data"

then you should have the following folders in the data directory:
1. db
2. log
3. backup

then run the command prompt in the administrators mode
1. press win+r and type cmd
2. hit ctrl+shift+enter(command prompt will be opened in the administrators mode)
3. change directory to the bin folder of the mongodb and type the below command:
    >mongodump --dbpath /data/db --out /data/backup/

MongoDB - Installation

Installation Of MongoDB:

Download MongoDB from official site. here is the link:
 Download MongoDB for Windows

As Java and .NET requires work space for the projects. MongoDB requires a Folder for backup.
create a folder "data".In "data" folder create another folder named "db".

 create these folders in the folder where MongoDB. if created in any other location, then it should be set by the dbpath settings.

1. creating the folders by command prompt:
  c:\ > md data -- creating the folder/directory named "data"
  c:\>cd data -- changing the directory to the data
  c:\data>md db-- creating db folder in data folder
  c:\data>cd db-- changing the db
  c:\data\db> -- db folder in data folder....

2. Now go to the bin folder of the "MongoDB" where "mongod.exe" is present. copy the path and paste it in the command prompt .
Ex: c:\MongoDB\bin\mongod.exe
then type the following command to specify the directory to the MongoDB.
c:\MongoDB\bin\mongod.exe --dbpath c:\data

now your directory is ready.

we need two instances of command prompt to run for server and other for database.

3. first run the mongod.exe which starts the server. it should not be closed till you are using database instance
4. second in another command prompt run mongo.exe.

c:\mongobd\bin\mongod.exe in first command prompt.
c:\mongodb\bin\mongo.exe in second command prompt.






MongoDB - Short Introduction

hey!!! wanna have a look at the new databases, MongoDb is one of them. The traditional databases are based on SQL, the big giants like Oracle,MS-SQL server etc uses SQL approach. since from 1970's the traditional databases are ruling the world. Big Data made the market competitors to move to the concept like No-SQL (it's not only SQL). 

Big Data: a simple definition is "data producing enormously in bytes(can say in Peta bytes)". Traditional databases are failing , it's better to say the performance and managing of data through traditional databases is becoming overhead. Hadoop is the solution for Big Data 

The IT market provides more number of NoSQL databases. MongoDB is one of them. It is Document Type database. the data is stored in J SON Format as Documents. It is quite opposite to the Relational Databases. the querying is easy. It is simple compared to other traditional databases.

Some of the key points of the MongoDB: 

1. there are no tables in the MongoDB.
2. it has the feature of Dynamic Schema.
3. it is in the format of JSON
4. Changing of Structure of Documents is easy. 
5. No Joins. Only uses Embedded documents and linking.