Sunday, July 30, 2017

Getting Started with MongoDB

The NoSQL database movement came about to address the shortcomings of relational databases and the demands of modern software development.  new data is unstructured and semi-structured, so developers also need a database that is capable of efficiently storing it. Unfortunately, the rigidly defined, schema-based approach used by relational databases makes it impossible to quickly incorporate new types of data, and is a poor fit for unstructured and semi-structured data. NoSQL provides a data model that maps better to these needs.

MongoDB is an open-source document database that provides high performance, high availability, and automatic scaling across a configurable set of systems that function as storage nodes.
 
  • database holds a set of collections
  • collection holds a set of documents
  • document is a set of fields
  • field is a key-value pair
  • key is a name (string)
  • value is a - basic type like string, integer, float, timestamp, binary, etc.,
  • a document, or an array of value
MongoDB Architecture
 MongoDB stores all data in documents, which are JSON-style data structures composed of field-and-value pairs. MongoDB stores documents on disk in the BSON serialization format. BSON is a binary representation of JSON documents, though it contains more data types than JSON. These documents can be simple documents as above and can also be complex documents such as below:

{
    id: x,
    name: y,
    other: z,
    multipleArray: [
        {lab1: "A",  lab2: "B", lab3:"C"},
        {lab1: "AB", lab2: "BB", lab3:"CB"},
        {lab1: "AC", lab2: "BC", lab3:"CC"}
    ]
}

Document Database

A record in MongoDB is a document, which is a data structure composed of field and value pairs. MongoDB documents are similar to JSON objects. The values of fields may include other documents, arrays, and arrays of documents.

The advantages of using documents are:
  • Documents (i.e. objects) correspond to native data types in many programming languages.
  • Embedded documents and arrays reduce need for expensive joins.
  • Dynamic schema supports fluent polymorphism.
Most user-accessible data structures in MongoDB are documents, including:
-> All database records.
-> Query selectors, which define what records to select for read, update, and delete operations.
-> Update definitions, which define what fields to modify during an update.
-> Index specifications, which define what fields to index.
-> Data output by MongoDB for reporting and configuration, such as the output of the server-status and the replica set configuration document.

Joins and Other Aggregation Enhancements in MongoDB 3.2 on-wards

How to create database and collections with basic examples to query  ?

spb@spb-VirtualBox:~$ mongo
MongoDB shell version: 3.2.12
connecting to: test
Server has startup warnings:
> show dbs
finance  0.000GB
local    0.000GB
mydb     0.000GB
MongoDB didn’t provide any command to create “database“. Actually, you don’t need to create it manually, because, MangoDB will create it on the fly, during the first time you save the value into the defined collection (or table in SQL), and database.

> use hospital
switched to db hospital
> db.patient.save({name:"John",age:"29",gender:"M",disease:"fever",city:"chennai"})
WriteResult({ "nInserted" : 1 })
> db.patient.save({name:"Ramesh",age:"55",gender:"M",disease:"blood pressure",city:"bengaluru"})
WriteResult({ "nInserted" : 1 })
> db.patient.save({name:"Harish",age:"35",gender:"M",disease:"fever",city:"bengaluru"})
WriteResult({ "nInserted" : 1 })
> db.patient.save({name:"Namitha",age:"25",gender:"F",disease:"fever",city:"bengaluru"})
WriteResult({ "nInserted" : 1 })
> db.patient.save({name:"Asha",age:"15",gender:"F",disease:"fever",city:"bengaluru"})
WriteResult({ "nInserted" : 1 })
> db.patient.save({name:"Ravi",age:"23",gender:"M",disease:"diabetic",city:"chennai"})
WriteResult({ "nInserted" : 1 })
> db.patient.save({name:"Lokesh",age:"37",gender:"M",disease:"fever",city:"mumbai"})
WriteResult({ "nInserted" : 1 })
> db.patient.save({name:"Sangeetha",age:"37",gender:"F",disease:"fever",city:"mumbai"})
WriteResult({ "nInserted" : 1 })
> db.patient.save({name:"Apoorva",age:"27",gender:"F",disease:"fever",city:"bengaluru"})
WriteResult({ "nInserted" : 1 })
> db.patient.save({name:"Jijo",age:"30",gender:"M",disease:"fever",city:"bengaluru"})
WriteResult({ "nInserted" : 1 })
> db.patient.save({name:"Mallik",age:"38",gender:"M",disease:"fever",city:"bengaluru"})
WriteResult({ "nInserted" : 1 })
> db.patient.save({name:"Parashuram",age:"32",gender:"M",disease:"fever",city:"bengaluru"})
WriteResult({ "nInserted" : 1 })
> db.patient.save({name:"Rakesh",age:"35",gender:"M",disease:"cold",city:"bengaluru"})
WriteResult({ "nInserted" : 1 })


> db.patient.find()
{ "_id" : ObjectId("597d83f6a9d2632baed3c076"), "name" : "John", "age" : "29", "gender" : "M", "disease" : "fever", "city" : "chennai" }
{ "_id" : ObjectId("597d8457a9d2632baed3c077"), "name" : "Ramesh", "age" : "55", "gender" : "M", "disease" : "blood pressure", "city" : "bengaluru" }
{ "_id" : ObjectId("597d8488a9d2632baed3c078"), "name" : "Harish", "age" : "35", "gender" : "M", "disease" : "fever", "city" : "bengaluru" }
{ "_id" : ObjectId("597d84baa9d2632baed3c079"), "name" : "Namitha", "age" : "25", "gender" : "F", "disease" : "fever", "city" : "bengaluru" }
{ "_id" : ObjectId("597d84efa9d2632baed3c07a"), "name" : "Asha", "age" : "15", "gender" : "F", "disease" : "fever", "city" : "bengaluru" }
{ "_id" : ObjectId("597d851aa9d2632baed3c07b"), "name" : "Ravi", "age" : "23", "gender" : "M", "disease" : "diabetic", "city" : "chennai" }
{ "_id" : ObjectId("597d8544a9d2632baed3c07c"), "name" : "Lokesh", "age" : "37", "gender" : "M", "disease" : "fever", "city" : "mumbai" }
{ "_id" : ObjectId("597d855ca9d2632baed3c07d"), "name" : "Sangeetha", "age" : "37", "gender" : "F", "disease" : "fever", "city" : "mumbai" }
{ "_id" : ObjectId("597d8571a9d2632baed3c07e"), "name" : "Apoorva", "age" : "27", "gender" : "F", "disease" : "fever", "city" : "bengaluru" }
{ "_id" : ObjectId("597d858ba9d2632baed3c07f"), "name" : "Jijo", "age" : "30", "gender" : "M", "disease" : "fever", "city" : "bengaluru" }
{ "_id" : ObjectId("597d859da9d2632baed3c080"), "name" : "Mallik", "age" : "38", "gender" : "M", "disease" : "fever", "city" : "bengaluru" }
{ "_id" : ObjectId("597d85afa9d2632baed3c081"), "name" : "Parashuram", "age" : "32", "gender" : "M", "disease" : "fever", "city" : "bengaluru" }
{ "_id" : ObjectId("597d85c7a9d2632baed3c082"), "name" : "Rakesh", "age" : "35", "gender" : "M", "disease" : "cold", "city" : "bengaluru" }
----------------------------
 > show dbs
finance   0.000GB
hospital  0.000GB
local     0.000GB
mydb      0.000GB

----------------------------------------------------------------------
To query the document on the basis of some condition, you can use following operations.

1) query to get records  where  desease=fever
 > db.patient.find({"disease":"fever"})
{ "_id" : ObjectId("597d83f6a9d2632baed3c076"), "name" : "John", "age" : "29", "gender" : "M", "disease" : "fever", "city" : "chennai" }
{ "_id" : ObjectId("597d8488a9d2632baed3c078"), "name" : "Harish", "age" : "35", "gender" : "M", "disease" : "fever", "city" : "bengaluru" }
{ "_id" : ObjectId("597d84baa9d2632baed3c079"), "name" : "Namitha", "age" : "25", "gender" : "F", "disease" : "fever", "city" : "bengaluru" }
{ "_id" : ObjectId("597d84efa9d2632baed3c07a"), "name" : "Asha", "age" : "15", "gender" : "F", "disease" : "fever", "city" : "bengaluru" }
{ "_id" : ObjectId("597d8544a9d2632baed3c07c"), "name" : "Lokesh", "age" : "37", "gender" : "M", "disease" : "fever", "city" : "mumbai" }
{ "_id" : ObjectId("597d855ca9d2632baed3c07d"), "name" : "Sangeetha", "age" : "37", "gender" : "F", "disease" : "fever", "city" : "mumbai" }
{ "_id" : ObjectId("597d8571a9d2632baed3c07e"), "name" : "Apoorva", "age" : "27", "gender" : "F", "disease" : "fever", "city" : "bengaluru" }
{ "_id" : ObjectId("597d858ba9d2632baed3c07f"), "name" : "Jijo", "age" : "30", "gender" : "M", "disease" : "fever", "city" : "bengaluru" }
{ "_id" : ObjectId("597d859da9d2632baed3c080"), "name" : "Mallik", "age" : "38", "gender" : "M", "disease" : "fever", "city" : "bengaluru" }
{ "_id" : ObjectId("597d85afa9d2632baed3c081"), "name" : "Parashuram", "age" : "32", "gender" : "M", "disease" : "fever", "city" : "bengaluru" }
---------------------------------------------- ---------------------
2) To display the results in a formatted way with pretty() method to get records  where  desease=fever 

> db.patient.find({"disease":"fever"}).pretty()
{
    "_id" : ObjectId("597d83f6a9d2632baed3c076"),
    "name" : "John",
    "age" : "29",
    "gender" : "M",
    "disease" : "fever",
    "city" : "chennai"
}
{
    "_id" : ObjectId("597d8488a9d2632baed3c078"),
    "name" : "Harish",
    "age" : "35",
    "gender" : "M",
    "disease" : "fever",
    "city" : "bengaluru"
}
{
    "_id" : ObjectId("597d84baa9d2632baed3c079"),
    "name" : "Namitha",
    "age" : "25",
    "gender" : "F",
    "disease" : "fever",
    "city" : "bengaluru"
}
{
    "_id" : ObjectId("597d84efa9d2632baed3c07a"),
    "name" : "Asha",
    "age" : "15",
    "gender" : "F",
    "disease" : "fever",
    "city" : "bengaluru"
}
{

    "_id" : ObjectId("597d8544a9d2632baed3c07c"),
    "name" : "Lokesh",
    "age" : "37",
    "gender" : "M",
    "disease" : "fever",
    "city" : "mumbai"
}
{
    "_id" : ObjectId("597d855ca9d2632baed3c07d"),
    "name" : "Sangeetha",
    "age" : "37",
    "gender" : "F",
    "disease" : "fever",
    "city" : "mumbai"
}
{
    "_id" : ObjectId("597d8571a9d2632baed3c07e"),
    "name" : "Apoorva",
    "age" : "27",
    "gender" : "F",
    "disease" : "fever",
    "city" : "bengaluru"
}
{
    "_id" : ObjectId("597d858ba9d2632baed3c07f"),
    "name" : "Jijo",
    "age" : "30",
    "gender" : "M",
    "disease" : "fever",
    "city" : "bengaluru"
}
{
    "_id" : ObjectId("597d859da9d2632baed3c080"),
    "name" : "Mallik",
    "age" : "38",
    "gender" : "M",
    "disease" : "fever",
    "city" : "bengaluru"
}
{
    "_id" : ObjectId("597d85afa9d2632baed3c081"),
    "name" : "Parashuram",
    "age" : "32",
    "gender" : "M",
    "disease" : "fever",
    "city" : "bengaluru"
}
-------------------------------------------------------
3) query to get records  where  age=25
> db.patient.find({"age":"25"})
{ "_id" : ObjectId("
597d84baa9d2632baed3c079"), "name" : "Namitha", "age" : "25", "gender" : "F", "disease" : "fever", "city" : "bengaluru" }
>
-------------------------------------------------------
4) query to get records  where  age greater than 25

> db.patient.find({"age":{$gt:"25"}})
{ "_id" : ObjectId("597d83f6a9d2632baed3c076"), "name" : "John", "age" : "29", "gender" : "M", "disease" : "fever", "city" : "chennai" }
{ "_id" : ObjectId("597d8457a9d2632baed3c077"), "name" : "Ramesh", "age" : "55", "gender" : "M", "disease" : "blood pressure", "city" : "bengaluru" }
{ "_id" : ObjectId("597d8488a9d2632baed3c078"), "name" : "Harish", "age" : "35", "gender" : "M", "disease" : "fever", "city" : "bengaluru" }
{ "_id" : ObjectId("597d8544a9d2632baed3c07c"), "name" : "Lokesh", "age" : "37", "gender" : "M", "disease" : "fever", "city" : "mumbai" }
{ "_id" : ObjectId("597d855ca9d2632baed3c07d"), "name" : "Sangeetha", "age" : "37", "gender" : "F", "disease" : "fever", "city" : "mumbai" }
{ "_id" : ObjectId("597d8571a9d2632baed3c07e"), "name" : "Apoorva", "age" : "27", "gender" : "F", "disease" : "fever", "city" : "bengaluru" }
{ "_id" : ObjectId("597d858ba9d2632baed3c07f"), "name" : "Jijo", "age" : "30", "gender" : "M", "disease" : "fever", "city" : "bengaluru" }
{ "_id" : ObjectId("597d859da9d2632baed3c080"), "name" : "Mallik", "age" : "38", "gender" : "M", "disease" : "fever", "city" : "bengaluru" }
{ "_id" : ObjectId("597d85afa9d2632baed3c081"), "name" : "Parashuram", "age" : "32", "gender" : "M", "disease" : "fever", "city" : "bengaluru" }
{ "_id" : ObjectId("597d85c7a9d2632baed3c082"), "name" : "Rakesh", "age" : "35", "gender" : "M", "disease" : "cold", "city" : "bengaluru" }
------------------------------------------------------------------------
5) query to get records  where  age less than 25
 
> db.patient.find({"age":{$lt:"25"}})
{ "_id" : ObjectId("597d84efa9d2632baed3c07a"), "name" : "Asha", "age" : "15", "gender" : "F", "disease" : "fever", "city" : "bengaluru" }
{ "_id" : ObjectId("597d851aa9d2632baed3c07b"), "name" : "Ravi", "age" : "23", "gender" : "M", "disease" : "diabetic", "city" : "chennai" }
---------------------------------------------------------------------
6) query to get records  where  age less than or equal to   25
 
 > db.patient.find({"age":{$lte:"25"}})
{ "_id" : ObjectId("597d84baa9d2632baed3c079"), "name" : "Namitha", "age" : "25", "gender" : "F", "disease" : "fever", "city" : "bengaluru" }
{ "_id" : ObjectId("597d84efa9d2632baed3c07a"), "name" : "Asha", "age" : "15", "gender" : "F", "disease" : "fever", "city" : "bengaluru" }
{ "_id" : ObjectId("597d851aa9d2632baed3c07b"), "name" : "Ravi", "age" : "23", "gender" : "M", "disease" : "diabetic", "city" : "chennai" }
----------------------------------------------------------------------------
7) query to get records  where  age greater than or equal to   25
> db.patient.find({"age":{$gte:"25"}})
{ "_id" : ObjectId("597d83f6a9d2632baed3c076"), "name" : "John", "age" : "29", "gender" : "M", "disease" : "fever", "city" : "chennai" }
{ "_id" : ObjectId("597d8457a9d2632baed3c077"), "name" : "Ramesh", "age" : "55", "gender" : "M", "disease" : "blood pressure", "city" : "bengaluru" }
{ "_id" : ObjectId("597d8488a9d2632baed3c078"), "name" : "Harish", "age" : "35", "gender" : "M", "disease" : "fever", "city" : "bengaluru" }
{ "_id" : ObjectId("597d84baa9d2632baed3c079"), "name" : "Namitha", "age" : "25", "gender" : "F", "disease" : "fever", "city" : "bengaluru" }
{ "_id" : ObjectId("597d8544a9d2632baed3c07c"), "name" : "Lokesh", "age" : "37", "gender" : "M", "disease" : "fever", "city" : "mumbai" }
{ "_id" : ObjectId("597d855ca9d2632baed3c07d"), "name" : "Sangeetha", "age" : "37", "gender" : "F", "disease" : "fever", "city" : "mumbai" }
{ "_id" : ObjectId("597d8571a9d2632baed3c07e"), "name" : "Apoorva", "age" : "27", "gender" : "F", "disease" : "fever", "city" : "bengaluru" }
{ "_id" : ObjectId("597d858ba9d2632baed3c07f"), "name" : "Jijo", "age" : "30", "gender" : "M", "disease" : "fever", "city" : "bengaluru" }
{ "_id" : ObjectId("597d859da9d2632baed3c080"), "name" : "Mallik", "age" : "38", "gender" : "M", "disease" : "fever", "city" : "bengaluru" }
{ "_id" : ObjectId("597d85afa9d2632baed3c081"), "name" : "Parashuram", "age" : "32", "gender" : "M", "disease" : "fever", "city" : "bengaluru" }
{ "_id" : ObjectId("597d85c7a9d2632baed3c082"), "name" : "Rakesh", "age" : "35", "gender" : "M", "disease" : "cold", "city" : "bengaluru" }
-------------------------------------------------------
8) query to get records  where  age NOT equal to   25
 
> db.patient.find({"age":{$ne:"25"}})
{ "_id" : ObjectId("597d83f6a9d2632baed3c076"), "name" : "John", "age" : "29", "gender" : "M", "disease" : "fever", "city" : "chennai" }
{ "_id" : ObjectId("597d8457a9d2632baed3c077"), "name" : "Ramesh", "age" : "55", "gender" : "M", "disease" : "blood pressure", "city" : "bengaluru" }
{ "_id" : ObjectId("597d8488a9d2632baed3c078"), "name" : "Harish", "age" : "35", "gender" : "M", "disease" : "fever", "city" : "bengaluru" }
{ "_id" : ObjectId("597d84efa9d2632baed3c07a"), "name" : "Asha", "age" : "15", "gender" : "F", "disease" : "fever", "city" : "bengaluru" }

{ "_id" : ObjectId("597d851aa9d2632baed3c07b"), "name" : "Ravi", "age" : "23", "gender" : "M", "disease" : "diabetic", "city" : "chennai" }
{ "_id" : ObjectId("597d8544a9d2632baed3c07c"), "name" : "Lokesh", "age" : "37", "gender" : "M", "disease" : "fever", "city" : "mumbai" }
{ "_id" : ObjectId("597d855ca9d2632baed3c07d"), "name" : "Sangeetha", "age" : "37", "gender" : "F", "disease" : "fever", "city" : "mumbai" }
{ "_id" : ObjectId("597d8571a9d2632baed3c07e"), "name" : "Apoorva", "age" : "27", "gender" : "F", "disease" : "fever", "city" : "bengaluru" }
{ "_id" : ObjectId("597d858ba9d2632baed3c07f"), "name" : "Jijo", "age" : "30", "gender" : "M", "disease" : "fever", "city" : "bengaluru" }
{ "_id" : ObjectId("597d859da9d2632baed3c080"), "name" : "Mallik", "age" : "38", "gender" : "M", "disease" : "fever", "city" : "bengaluru" }
{ "_id" : ObjectId("597d85afa9d2632baed3c081"), "name" : "Parashuram", "age" : "32", "gender" : "M", "disease" : "fever", "city" : "bengaluru" }
{ "_id" : ObjectId("597d85c7a9d2632baed3c082"), "name" : "Rakesh", "age" : "35", "gender" : "M", "disease" : "cold", "city" : "bengaluru" }
>  
 ----------------------------------------------------------------------------------------------- 
CRUD (Create Read Update Delete) operation we have following commands in the MongoDB 


source

 That’s all for  basic introduction  on MongoDB
Reference:
https://docs.mongodb.com/manual/introduction/
https://www.mongodb.com/leading-nosql-database
https://www.tutorialspoint.com/mongodb/mongodb_query_document.htm
http://theholmesoffice.com/how-to-create-a-mongodb-database/ 
https://www.codeproject.com/Articles/1037052/Introduction-to-MongoDB 
http://www.developer.com/java/data/getting-started-with-mongodb-as-a-java-nosql-solution.html

No comments:

Post a Comment