MONGODB - a nosql database


Before we start with mongodb , we have to know first why we need it ? We always use a SQL( structured query language) for database operations . Every thing in such databases is in tabular form. So if you have unknown set of data then how are you going to deal with it .

The most easy way is to use a unstructured language. mongobd gives us such support.
It takes data in JSON format and do its operations on it . No tables, no queries, no predefined format , not any need of known structure of data.
So mongodb is most preferrable way to create your database and it is also faster .We will understand steps in its creation as following --

Databases:

In MongoDB, databases hold collections of documents.
To select a database to use, in the mongo shell, issue the use <db> statement, as in the following example:
use myDB

Create a Database

If a database does not exist, MongoDB creates the database when you first store data for that database. As such, you can switch to a non-existent database and perform the following operation in the mongo shell:
use myNewDB

db.myNewCollection1.insertOne( { x: 1 } )
The insertOne() operation creates both the database myNewDB and the collection myNewCollection1 if they do not already exist.
For a list of restrictions on database names, see Naming Restrictions.

Collections

MongoDB stores documents in collections. Collections are analogous to tables in relational databases.

Create a Collection

If a collection does not exist, MongoDB creates the collection when you first store data for that collection.
db.myNewCollection2.insertOne( { x: 1 } )
db.myNewCollection3.createIndex( { y: 1 } )
Both the insertOne() and the createIndex() operations create their respective collection if they do not already exist.
For a list of restrictions on collection names, see Naming Restrictions.

Explicit Creation

MongoDB provides the db.createCollection() method to explicitly create a collection with various options, such as setting the maximum size or the documentation validation rules. If you are not specifying these options, you do not need to explicitly create the collection since MongoDB creates new collections when you first store data for the collections.
To modify these collection options, see collMod.

Document Validation

New in version 3.2.
By default, a collection does not require its documents to have the same schema; i.e. the documents in a single collection do not need to have the same set of fields and the data type for a field can differ across documents within a collection.
Starting in MongoDB 3.2, however, you can enforce document validation rules for a collection during update and insert operations. See Schema Validation for details.

Modifying Document Structure

To change the structure of the documents in a collection, such as add new fields, remove existing fields, or change the field values to a new type, update the documents to the new structure.
INSERTING DOCUMENTS :    When you have to insert a json data in the database.put it inside the following command.
db.mydatabase.insert([ add you JSON data here ])
FINDING DOCUMENTS: this is for search queries , which element of a document you want to retrieve, 
db.mydatabase.find()   // if you want to so every document
db.mydatabase.findOne()    //if you want to see one arbitrary document
db.mydatabase.find().pretty()   // if want document in well arranged manner
db.mydatabase.find({"name" : 1 , "id ": false})  //Shows only the names of the mydatabase

LIST THE AVAILABLE COLLECTIONS --(command)

show collections

UPDATING DOCUMENTS : in the command we first write the parameter we want to update and in next the updated parameter .

db.mydatabase.update({name : "previous name"}, {name : "update name"})

Removing Documents :  which parameter you  want to remove of a database .
db.ships.remove({name : 'USS Prometheus'})
WORKING WITH INDEX: when you read a book or dictionary you know the page number or jump to that page via index .. In mongodb we also assign index to a parameter which is unique , then perform "find()" operation ,it boots up the speed .
creating an index : --  db.ships.ensureIndex({name : 1})
Dropping an index : -- db.ships.dropIndex({name : 1})
Aggregation Examples :
db.ships.aggregate([{$group : {_id : "$operator", num_ships : {$sum : 1}}}])      /// Counts the number of ships per operator, would be in SQL: SELECT operator, count(*) FROM ships GROUP BY operator;
 db.ships.aggregate([{$project : {_id : 0, operator : {$toLower : "$operator"}, crew : {"$multiply" : ["$crew",10]}}}])  // Combination of $project-stage and $group-stage. Aggregation Expressions $sum Summing up values
 db.ships.aggregate([{$group : {_id : "$operator", num_ships : {$sum : "$crew"}}}]) 
$avg :--Calculating average values
 db.ships.aggregate([{$group : {_id : "$operator", num_ships : {$avg : "$crew"}}}]) 
$min / $max :--  Finding min/max values
 db.ships.aggregate([{$group : {_id : "$operator", num_ships : {$min : "$crew"}}}]) 
$push:-- Pushing values to a result array
 db.ships.aggregate([{$group : {_id : "$operator", classes : {$push: "$class"}}}]) 
$addToSet :--Pushing values to a result array without duplicates 
db.ships.aggregate([{$group : {_id : "$operator", classes : {$addToSet : "$class"}}}]) 
$first / $last :-- Getting the first / last document 
db.ships.aggregate([{$group : {_id : "$operator", last_class : {$last : "$class"}}}])

IF WE COMPARE IT WITH SQL :--



 WHERE            $match 
 GROUP BY        $group
 HAVING            $match
SELECT          $project 
 ORDER BY           $sort
 LIMIT                   $limit
 SUM                    $sum 
COUNT               $sum
JOIN              $unwind

Comments

Popular posts from this blog

Amazon Web Services

Google Code-In mentorship experience :)

Hacker Rank all java and python problem solutions