Wednesday, August 15, 2018

15. MongoDB : MongoDB Performance

15. MongoDB : MongoDB Performance



Commands to check STATS ###########################################


//execute below command from bin , before that u should have started mongod
//below files exist in bin folder
mongostat;   //check statistics
mongotop; // to track all read and write activity


db.serverStatus(); 
db.stats();    //number of collection, objects etc..
db.items.stats();  //collection level statistics


Journaling Mechanics ###########################################


- If we launch MongoD in Journal mode, then all write 1st goes to Journal before it goes to DB
     -- mongod.exe --dbpath D:\mongoDBDatabase --rest --journal
- Adv : fast and cheap (eg: while writing if server goes down, then when ever server comes up it takes from journal later)



Storage Engines ###########################################


1> memory map1 - read heavy operation , this is based on m-map files.
2> Wired-Tiger - heavy write operation, eg: facebook, twitter etc.
               - complex - updates possible
               - Uses commit chckpoint - after every 60 secs moving data from Journal to memory



Index - UnderIndex/OverIndex - poor design ###########################################


- All indexes are written to RAM, only if RAM is full then it writes to Harddisc which is very slow.
- Usually - in production 10% of Harddisck RAM shoud be there
  Eg: 500 GB HD,  50G GB RAM

Mongoos ###########################################


- Always use Mongoos tool - which handles all connection pool internally

Backup data ###########################################


- 1> copy paste all collections which is inside "D://mongoDBDatabase"   all "wt" files.
     always enable journal if you need backup
- 2> mongodump    //reads data from a mongDB database and creates BSON files. (it takes only documents as backup, it wont take index's, takes what index on what field)
     mongorestore   //


Lock DB during backup ###########################################


- use admin
- db.runCommand({"fsync":1, "lock":1});   //lock during backup

- db.$cmd.sys.unlock.findOne();    //unlock after backup is done.

14. MongoDB : Custom Search

14. MongoDB : Custom Search




Sample Data
{
"_id":0,
"name":"Pavan",
"results": [
{"evaluation":"term1","score":25.46},
{"evaluation":"term2","score":11.78},
{"evaluation":"term3","score":56.67}
]
}




1.      Find count and percentage of employees who failed in term 1, the passing score being 37
2.      Find employees who failed in aggregate (term1 + term2 + term3)
3.      Find the Average score of trainees for term1
4.      Find the Average score of trainees for aggregate (term1 + term2 + term3)    
5.      Find number of employees who failed in all the three (term1 + term2 + term3)
1.      Find the number of employees who failed in any of the three (term1 + term2 + term3)

SSample data:

cDatabase - "employee"

Collection - "training"

{"_id":0,"name":"Pavan","results":[{"evaluation":"term1","score":1.463179736705023},{"evaluation":"term2","score":11.78273309957772},{"evaluation":"term3","score":6.676176060654615}]}

{"_id":1,"name":"Vijay","results":[{"evaluation":"term1","score":60.06045071030959},{"evaluation":"term2","score":52.79790691903873},{"evaluation":"term3","score":71.76133439165544}]}
{"_id":2,"name":"amar","results":[{"evaluation":"term1","score":67.03077096065002},{"evaluation":"term2","score":6.301851677835235},{"evaluation":"term3","score":20.18160621941858}]}
{"_id":3,"name":"Girish","results":[{"evaluation":"term1","score":71.64343899778332},{"evaluation":"term2","score":24.80221293650313},{"evaluation":"term3","score":1.694720653897219}]}
{"_id":4,"name":"Shiv","results":[{"evaluation":"term1","score":78.68385091304332},{"evaluation":"term2","score":90.2963101368042},{"evaluation":"term3","score":34.41620148042529}]}
{"_id":5,"name":"Shobith","results":[{"evaluation":"term1","score":44.87186330181261},{"evaluation":"term2","score":25.72395114668016},{"evaluation":"term3","score":10.53058536508186}]}
{"_id":6,"name":"Bhavana","results":[{"evaluation":"term1","score":37.32285459166097},{"evaluation":"term2","score":28.32634976913737},{"evaluation":"term3","score":16.58341639738951}]}
{"_id":7,"name":"Monica","results":[{"evaluation":"term1","score":90.37826509157176},{"evaluation":"term2","score":42.48780666956811},{"evaluation":"term3","score":67.18328596625217}]}


################################################################################

package com.mongodb.customsearch;
import static java.util.Arrays.asList;

import java.util.ArrayList;
import java.util.Collection;

import org.bson.Document;

import com.mongodb.Block;
import com.mongodb.MongoClient;
import com.mongodb.client.AggregateIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
public class EmployeeTrainingScores {

public static void main(String[] args) {
try{
// Creating mongo client default connection host:localhost port:12027
//MongoClient mongoClient = new MongoClient();
MongoClient mongoClient = new MongoClient("localhost", 27017);
// fetching a database with name students
MongoDatabase db = mongoClient.getDatabase("employee");
// fetching collection scores from database students
MongoCollection collection = db.getCollection("training");
//find one record
findFailedStudInTerm1(collection);
//failed employees in aggregate (term1+term2+term3)
failedInAggregate(collection);
//average score of trainees in term1
averageScoreTerm1(collection);
//Average score of class for aggregate (term1 + term2 + term3)
averageClassScore(collection);
//count of employee failed in all three terms
employeeCountFailInAlLTerms(collection);
//count of employee failed in either of three terms
employeeCountFailAtleastATerm(collection);
mongoClient.close();
}catch(Exception exception){
        System.err.println(exception.getClass().getName() + ":" + exception.getMessage());
    }

}

/**
* function to find a record input : mongodb collection
* 1. Find count and percentage of employees who failed in term 1, the passing score being 37
*/
public static Long findFailedStudInTerm1(MongoCollection<Document> collection) {

Long count = collection.count(new Document("results.evaluation", "term1").append("results.score", new Document("$lt",37)));
Long totalStudents=collection.count();
Long per_Stud=(count*100/totalStudents);
System.out.println("############################# 1. Find count and percentage of employees who failed in term 1, the passing score being 37 #############################");
System.out.println("Number of students failed in exams in Term1: passing marks 37 ======> " + count);
System.out.println("Percentage of students failed in exams  in Term1: passing marks 37 ======> " + per_Stud +" %");
return per_Stud;
}
/**
* function to find a record input : mongodb collection
* 2. Find employees who failed in aggregate (term1 + term2 + term3)
*/
private static void failedInAggregate(MongoCollection collection) {
System.out.println("");
System.out.println("############################# 2. Find employees who failed in aggregate (term1 + term2 + term3) #############################");
//term1 + term2 + term3
//37 + 37 + 37 = 111
Collection<Document> employees = collection.aggregate(asList(new Document("$unwind","$results"),new Document("$group", new Document("_id","$name").append("total", new Document("$sum","$results.score"))),new Document("$match", new Document("total",new Document("$lt",111))))).into(new ArrayList<Document>());
for(Document doc:employees){
System.out.println("employees who failed in aggregate (term1+ term2 + term3) : " + doc.toJson());
}
}
/**
* function to find a record input : mongodb collection
* 3. Find the Average score of trainees for term1
*/
private static void averageScoreTerm1(MongoCollection collection) {
System.out.println("");
System.out.println("############################# 3. Find the Average score of trainees for term1 #############################");
Collection<Document> employees = collection.aggregate(asList( new Document("$unwind","$results"), new Document("$match",new Document("results.evaluation","term1")),new Document("$group",new Document("_id",null).append("Average", new Document("$avg","$results.score"))))).into(new ArrayList<Document>());
for(Document doc:employees){
System.out.println("Average score of trainees for term1 : " + doc.toJson());
}
}
/**
* function to find a record input : mongodb collection
* 4. Find the Average score of trainees for aggregate (term1 + term2 + term3)
*/
private static void averageClassScore(MongoCollection collection) {
System.out.println("");
System.out.println("############################# 4. Find the Average score of trainees for aggregate (term1 + term2 + term3)      #############################");
Collection<Document> employees = collection.aggregate(asList(new Document("$unwind","$results"),new Document("$group", new Document("_id","$name").append("Average", new Document("$avg","$results.score"))))).into(new ArrayList<Document>());
for(Document doc:employees){
System.out.println("Average score of trainees for aggregate (term1+ term2 + term3) : " + doc.toJson());
}
}
/**
* function to find a record input : mongodb collection
* 5. Find number of employees who failed in all the three (term1 + term2 + term3)
*/
private static void employeeCountFailInAlLTerms(MongoCollection collection) {
System.out.println("");
System.out.println("############################# 5. Find number of employees who failed in all the three (term1 + term2 + term3) #############################");
Long count=collection.count(new Document("results.0.score",new Document("$lt",37)).append("results.1.score",new Document("$lt",37)).append("results.2.score",new Document("$lt",37)));
System.out.println("Count of employees failing in all terms : " + count);
}
/**
* function to find a record input : mongodb collection
* 6. Find the number of employees who failed in any of the three (term1 + term2 + term3)
*/
private static void employeeCountFailAtleastATerm(MongoCollection collection) {
System.out.println("");
System.out.println("############################# 6. Find the number of employees who failed in any of the three (term1 + term2 + term3) #############################");
Long count=collection.count(new Document("$or",asList(new Document("results.0.score",new Document("$lt",37)), new Document("results.1.score",new Document("$lt",37)),new Document("results.2.score",new Document("$lt",37)))));
System.out.println("Count of employees failing in either of the terms : " + count);
}





}




################################################################################


13. MongoDB : Java program to Insert / Retrieve the documents

13. MongoDB : Java program to Insert / Retrieve / Update / Delete the documents



*************************************************************
1> MongoClient mongoClient = new MongoClient("localhost", 27017);
2> MongoDatabase db = mongoClient.getDatabase("test");
3> MongoCollection<Document> collection = db.getCollection("java_item");
4> Document document = new Document("title", "MongoDB").append("description", "database").append("likes", 100).append("name","Pavan");

5> collection.insertOne(document);
***************************************************************



///////////////////////////////////////////INSERT ONE DOCUMENT #############################

package com.mongodb.createdocuments;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
public class InsertDocuments {
    public static void main(String[] args) {

        // To connect to the mongoDB server
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        // Now connect to your database, in mogoDB 4.0, DB has changed to MongoDatabase
        MongoDatabase db = mongoClient.getDatabase("test");
        System.out.println("Connection to the database successful.");
        MongoCollection<Document> collection = db.getCollection("java_item");
        System.out.println("Collection item selected successfully.");
        Document document = new Document("title", "MongoDB").append("description", "database").append("likes", 100).append("name","Pavan");
        collection.insertOne(document);
        System.out.println("Document inserted successfully.");
        mongoClient.close();

    }
}

#############################RETRIEVE DOCUMENTS####################################################

package com.mongodb.retrievedocuments;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
//import com.mongodb.client.MongoDatabase;
public class RetrievedDocuments {
   
    public static void main(String[] args) {
        // To connect to the mongoDB server
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        // Now connect to your database
        DB db = mongoClient.getDB("test");
        //MongoDatabase db = mongoClient.getDatabase("test");
        System.out.println("Connection to the database successful.");
        DBCollection collection = db.getCollection("items");
        System.out.println("Collection items selected successfully.");
        DBCursor cursor = collection.find();
        int index = 1;
        while (cursor.hasNext()) {
            System.out.println("Inserted document: " + index);
            System.out.println(cursor.next());
            index++;
        }
        System.out.println("Documents retrieved successfully.");
        mongoClient.close();
    }
}

#############################UPDATE DOCUMENTS####################################################

package com.mongodb.updateddouments;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
public class UpdateDocuments {
    public static void main(String[] args) {
        try{
        // To connect to the mongoDB server
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        // Now connect to your database
        DB db = mongoClient.getDB("test");
        System.out.println("Connection to the database successful.");
        DBCollection collection = db.getCollection("java_item");
        DBCursor cursor = collection.find();
        System.out.println("Collection items selected successfully.");
        while(cursor.hasNext()){
        DBObject updateDocument = cursor.next();
        updateDocument.put("likes", 300);
        BasicDBObject query = new BasicDBObject().append("title", "MongoDB");
        collection.update(query, updateDocument, false, false);
        }
        System.out.println("Document updated successfully.");
        cursor = collection.find();
        int index=1;
        while(cursor.hasNext()){
            System.out.println("Updated Document: " + index);
            System.out.println(cursor.next());
            index++;
        }
        mongoClient.close();
        }catch(Exception exception){
            System.err.println(exception.getClass().getName() + ":" + exception.getMessage());
        }
    }

}

#############################DELETE DOCUMENTS####################################################

package com.mongodb.deletedocument;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
public class DeleteDocument {
   
    public static void main(String[] args) throws InterruptedException {
        // To connect to the mongoDB server
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        // Now connect to your database
        DB db = mongoClient.getDB("test");
        System.out.println("Connection to the database successful.");
        DBCollection collection = db.getCollection("java_item");
        System.out.println("Collection items selected successfully.");
        BasicDBObject searchQuery = new BasicDBObject().append("title", "MongoDB");
        collection.remove(searchQuery);
        DBCursor cursor = collection.find();
        int index = 1;
        while (cursor.hasNext()) {
            System.out.println("Inserted document: " + index);
            System.out.println(cursor.next());
            index++;
        }
        System.out.println("Documents retrieved successfully.");
        mongoClient.close();
    }
}









12. MongoDB : GridFS - to store (static data, audio, video large files)

12. MongoDB : GridFS - to store (static data, audio, video large files)



**************************************************************************
//save file on db
GridFSInputFile gfsFile = gfsPhoto.createFile(imageFile);
gfsFile.save();

//get all grid files
DBCursor cursor = gfsPhoto.getFileList();

//get particular grid file
GridFSDBFile imageForOutput = gfsPhoto.findOne(newFileName);

//remove grid file from db
gfsPhoto.remove(gfsPhoto.findOne(newFileName));

**************************************************************************
bez we know 16MB is the max in document.
GridFS -> how it stores more than 16MB, internally it split in to chunks and stores



###############################JAVA PROGRAM TO STORE/READ/DELETE GRIDFILE FROM DB###############################################

-- Download JDK 1.8 (Version that you prefer) for Windows/Linux
-- Install Eclipse oxygen
-- Create a project in eclipse
-- Create a simple Java Class in eclipse
-- Use the code below to upload an image file
-- Get these dependencies and add it to the java classes build path

mongodb-driver-core-3.4.2.jar
mongodb-driver-3.4.2.jar
bson-3.4.2.jar

https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver/3.4.2   -> mongodb-driver-core-3.4.2.jar
https://mvnrepository.com/artifact/org.mongodb/mongodb-driver/3.4.2   -> mongodb-driver-3.4.2.jar
https://mvnrepository.com/artifact/org.mongodb/bson/3.4.2    -> bson-3.4.2.jar


##############################################################################

package com.mongodb.gridfs;
import java.io.File;
import java.io.IOException;
import com.mongodb.DB;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;
public class UploadFile {
    public static void main(String[] args) throws IOException {

        MongoClient mongoClient = new MongoClient("localhost", 27017);
        DB db = mongoClient.getDB("sample");
        String newFileName = "gridFS-java-image";
        File imageFile = new File("C:\\Users\\Pictures\\MyFile.png");
        // create a "photo" namespace
        GridFS gfsPhoto = new GridFS(db, "photo");
        // get image from local drive
        GridFSInputFile gfsFile = gfsPhoto.createFile(imageFile);
        // set the new file name for identity
        gfsFile.setFilename(newFileName);
        // save the image file to mongoDB
        gfsFile.save();
        // print the result
        DBCursor cursor = gfsPhoto.getFileList();

while (cursor.hasNext()){
            System.out.println(cursor.next());
        }
        // get the image file by it's filename
        GridFSDBFile imageForOutput = gfsPhoto.findOne(newFileName);
        // save it to a new image file
        imageForOutput.writeTo("C:\\Users\\Pictures\\MyFileFromMongDB.png");
        System.out.println("Done");
        // Remove the file.
        gfsPhoto.remove(gfsPhoto.findOne(newFileName));
        // close the connection
        mongoClient.close();
    }
}

##############################################################################

11. MongoDB : CAP COLLECTION & TTL Index.

11. MongoDB : CAP COLLECTION & TTL Index.


CAP COLLECTION - Create collection with fixed size. ######################################

Why we need this kind of collections ...? 
- Eg: Server Log - we dont want to keep historical data, as soon as new data comes in old data has to delete.

//max size of 5242880 bytes, and 5000 documents
db.createCollection("webserverlog", {capped:true,size:5242880,max:5000});
show collections
db.webserverlog.isCapped()
db.runCommand({"convertToCapped":"items",size:100000});  //to convert to cap collection
db.items.isCapped()





//How to delete the documents
TTL Index (Time to live) ###########################################
-----------------------------
//document will be deleted after (createdDate + 3600 sec), eg: ('May 30, 2015 14:00:00' + 3600 sec)
db.webserverlog.createIndex({"createdDate":1},{expireAfterSeconds:3600});

//document will be deleted after (expireDate + 0 sec), eg: ('June 30, 2015 14:00:00' + 0 sec)
db.webserverlog.createIndex({"expireDate":1},{expireAfterSeconds:0});
db.webserverlog.insert({"expireDate":new Date('June 30, 2015 14:00:00')});



10. MongoDB : Sharding

10. MongoDB : Sharding



9. MongoDB : Replication

9. MongoDB : Replication



8. MongoDB : Aggregate, MapReduce & Group

8. MongoDB : Aggregate, MapReduce & Group


Aggregate ############################################


//Calculate the total transaction that customer did till now (consider only transaction >= 10000)
db.customer_info.aggregate([
                        {$match:{"transaction": {$gte:100000}}},    //consider only records gte=100000
{$group:{{_id: "Cust_Id", transaction: {$sum:"$amount"}}}},   //if Cust_Id is same add the transaction
{$sort:"trasaction":1}}]);


Another Eg:



db.items.count();   //6

db.items.distinct("item");   //[ "pen", "pencil", "books" ]

db.items.count({item: "pen"});  //2

MapReduce ###########################################

Map reduce: (Looks same as Aggregation, but this works parallely)
----------

var mapFunction = function() {emit(this.customer_name,this.amount)}; // customer_name, amount are fields in customer_info.

var reduceFunction = function(customer_name,arrayOfAmounts){
return Array.sum(arrayOfAmounts);
}

//using mapFunction and educeFunction => it generates mapReduce_result table with final o/p
db.customer_info.mapReduce(mapFunction,reduceFunction,{out: "mapReduce_result"});
show tables; //you see - mapReduce_result
db.mapReduce_result.find();  //o/p will be reduced o/p





Group ############################################

//display totalAmount of particular customer_name = "John"
db.customer_info.group({key: {"customer_name":1},      //group on customer_name
                        cond: {"customer_name": "John"},            //get only "John" customer
                        reduce:function(curr,result)                        //reduce: add all the amount
                                         {result.amount += curr.amount;}, 
                        intial: {amount:0}})


7. MongoDB : GEO JSON & Geospatial Index

7. MongoDB : GEO JSON & Geospatial Index








//GEO JSON DATA ########################################################


/GEO JSON - which contains geography informations. - and location: {type: "point", coordinates: [-78.97,40.77]} this kind of data should be must.

db.createCollection("places");

db.places.insert({location: {type: "Point", coordinates: [12.925397371881461,77.63657815315337]}, name:"BangalorePizza_1", category:"PizzaShop"});
db.places.insert({location: {type: "Point", coordinates: [12.915442118877891,77.63820893631903]}, name:"BangalorePizza_2", category:"PizzaShop"});
db.places.insert({location: {type: "Point", coordinates: [12.916989810315489,77.63220078798098]}, name:"BangalorePizza_3", category:"PizzaShop"});

db.places.insert({location: {type: "Point", coordinates: [12.330624740837576,76.62695926148842]}, name:"MysorePizza_1", category:"PizzaShop"});
db.places.insert({location: {type: "Point", coordinates: [12.325232832287508,76.63530303596097]}, name:"MysorePizza_2", category:"PizzaShop"});

db.places.insert({location: {type: "Point", coordinates: [12.978403513741311,77.56954207070805]}, name:"Bangalore Railway Station1", category:"RailwayStation"});
db.places.insert({location: {type: "Point", coordinates: [12.978063446690769,77.57170478534276]}, name:"Bangalore Railway Station22", category:"RailwayStation"});




//GEO JSON INDEX ########################################################

//Geospatial Index
//to query , 1st we have to create index
//compound index -> geoIndex with normalIndex - YES possible
//compound index -> multiple geoIndex - NO not possible.
//db.collection.createIndex({<location field> : "2dsphere"}) is mandatory

db.places.createIndex({location: "2dsphere", category: -1, name:1});
db.places.createIndex({category: -1, name:1, location: "2dsphere"});
db.places.createIndex({location: "2dsphere"});

db.places.getIndexes();

//GEO JSON FIND ########################################################

db.places.find(
   { location: { $geoWithin: { $center: [ [12.91740963747045,77.63365255325667], 10 ] } } }
)

db.places.find(
   {category:"PizzaShop",location: { $geoWithin: { $center: [ [12.91740963747045,77.63365255325667], 10 ] } }}
)

db.places.find({location: {$geoWithin:{$box:[[-100,74.00],[10,100]}}})
db.places.find({location: {$geoWithin:{$geometry:{type:"Polygon", coordinates:[[[0,0],[-90,45.00],[-100,-50.00],[


     








6. MongoDB : Work with Index

6. MongoDB : Work with Index

*******************************************************************************
db.items.createIndex({..  ,  ..}, {.....}
                                                 {name:"myOwnIndex_name"}
                                                 {unique:true}
                                                 {default_languate:"spanish"}
                                                 {background:true}

db.items.dropIndex({...}
db.items.find({....}).hint({item:1})
db.items.find({....}).hint({$natural:1})
*******************************************************************************

############################################################### COLLECTION & DOCUMENT###############################################################


user test;

db.createCollection("items"); //create collection, collection or table name is "items" here

//it contains embedded document (details)
db.items.insert({"item":"Book","available":true,"soldQty":144821,"category":"NoSQl","details":{"ISDN":"1234","publisher":"XYZ Company"},"onlineSale":true});

db.items.find({"item":"Book"});
db.items.find({"item":"Book"}).pretty();

############################################################### INDEX###############################################################


create Normal Index.#####################################################

db.items.createIndex({"item":1}); //accending
db.items.createIndex({"item":-1}); //decending

db.items.getIndexes(); //list out all indexes

db.items.dropIndexes();
db.items.dropIndex("item");

//INDEX ON EMBEDDED DOCUMENT ###########################################

db.items.createIndex({"details.ISDN":1})

COMPOUND INDEX #################################################

db.items.createIndex({"item":1,"available":-1});
//index name = item_1_available_1

COMPOUND INDEX with ARRAY AND FIELD##############################
//here courses is array, and name is field
//creating index on array -> MangoDB - creates index on each element in array, so we should be very care full while creating

db.items.createIndex({"courses":1,"name":-1})

HASHED INDEX#############################################

db.items.createIndex({item:"hashed"})

unique INDEX #################################################
//unique ness on value inside document.

db.items.createIndex({"item":1},{unique:true});

TEXT INDEX#############################################
//we can give our own name for index
db.items.createIndex({item:"text"},{name:"item_index"})

TEXT INDEX - default language ##############################

db.customer_info.createIndex({"item":"text"},{default_languate:"spanish"})

INDEX CREATING IN BACKGROUND ##############################
//key like "item" need not to be in  " ".... (Its not mandatory)

db.items.createIndex({item:1},{background:true});

DROP INDEX ######################################

db.items.dropIndex({"item":1})
db.items.dropIndexes()

MONITOR INDEX GOING STATUS AND KILL ##############################

db.currentOp() //to monitor index going status
db.killOp()

PRINT ALL INDEXES  FROM ALL COLLECTIONS ##############################
//Note: we have db.items.getIndexes() -> which gets all index for particular colletion
//to get all indexes from all collection we have to for loop and print as shown below.

db.getCollectionNames().forEach(
function(collection)
   {
      indexes = db[collection].getIndexes();
      print("indexes for collection :"+collection+" : ");
      printjson(indexes);
    }
)

TO CHECK THE SQL QUERY USED FOR INDEX CREATION##############################

db.collection.explain() OR  cursor.explain()

FEW HELP QUIRES ##############################

help

db.items.help()

db.items.storageSize()
db.items.totalIndexSize()
db.items.reIndex()
db.printCollectionStats()

FIND WITH HINT COMMAND ##############################
//this is to tell mangodb to use only index mentioned in the hint while finding the records

//here to use "item" index using hint
db.items.find({item: "Book", available: true }).hint({item:1})

//here not to use any index - using $natural
db.items.find({item: "Book", available: true }).hint({$natural:1}).explain("executionStats")












5. MongoDB : Work with (Database, Collection and Documents)

5. MongoDB : Work with (Database, Collection and Documents)

********************************************************************
db.createCollection(...)
db.items.insert(...);       //{....}
db.items.find(...);         // {....}, $lt, $lte, $gte, $in, $or, /...,  $slice,  $elemMatch, 
db.items.update(...)      // {....}. $set
********************************************************************

show dbs;     //show all existing db's
use test;        //to switch to test db, if test does not exist then it creates and then switch

show collections;
db.createCollection("items");

//insert  ####################################################

db.items.insert({"name":"Sumit","courses":["mongodb","AWS","GCP"]});  //create document inside collection

db.items.find();
db.items.find({"name":"Sumit"});
db.items.find({"name":"Sumit"}).pretty();

//Bulk insert, if we dont give _id -> then it mongodb takes some unique id
var bulk = db.items.initializeOrderedBulkOp();
bulk.insert({_id:1, item:"pen", available:true, soldQty:700});
bulk.insert({_id:2, item:"pencil", available:false, soldQty:900});
bulk.insert({_id:3, item:"books", available:true, soldQty:700});
bulk.execute();

var bulk = db.items.initializeUnorderedBulkOp();
bulk.insert({_id:4, item:"pen", available:true, soldQty:700});
bulk.insert({_id:5, item:"pencil", available:false, soldQty:900});
bulk.insert({_id:6, item:"books", available:true, soldQty:700});
bulk.execute();


db.items.find();
db.items.find({available:true}).limit(5);  //limit(5) = row=5
db.items.findOne();
db.items.findOne({available:true});
db.items.find({available:true}).pretty();
db.items.find({soldQty: {$lt:800}});     //lessthan 800
db.items.find({soldQty: {$lte:800}, available:true});  //lessthan or equal to

db.items.find({available:{$in:[true,false]}});  //IN
db.items.find({available:true,soldQty:{$lt:900}}); //AND or ,
db.items.find({ $or: [{available:true},{soldQty:700}] }); //OR condition, note: within that mention in {},{}

//regular expression  ####################################################

//regular expression match, eg: in website search_______ anything in website
db.items.find({item:/pe/i});   //caseinsensitive //search anything which has 'pe' or 'i' in it
db.items.find({item:/Pen?/i}); //caseinsensitive //search anything which has 'pe' or 'i' in it

//array serch  ####################################################

db.itesm.find({country_codes:5});  //if country_codes is array - then search for '5' inside that.
db.itesm.find({country_codes.0:5});

//projection quries. (Only for Arrays : $elemMatch, $slice)

//here country_codes is array.
db.items.find({_id:5},{country_codes:{$slice:2}}); //shows only 1st 2 array elements, 
note: if you give {$slice:-2} then it shows last 2 elements of array.

db.itesm.find({country_codes:{$elemMatch:{$gte:3,$lte:6}}}); //between 3 and 6, inside an array elemMatch

//javascript inside query ####################################################


//javascript inside query - using "$where"
db.items.find({"$where":function().......});
db.items.find(({"$where": "this.x + this.y == 10"});
db.items.find(({"$where": "function() {return this.x + this.y == 10;}"});

//cursor, ####################################################


//cursor, light way of handling on dataset query.
var cursor = db.collection.find();
while(cursor.hasNext()) {
....obj= cursor.next();
.....
}
db.c.find.skip(3);
db.c.find().soft({username:1,age:-1})

//UPDATE RECORD ####################################################


//update document where item=Book
//Note: below taken is different example of "items" collection.

db.items.update({item:"Book"},{$set: {category: 'NoSQL', details: {ISDN: "1234", publisher:"XYZ"}}});

//update an embedded field
db.items.update({item: "Pen"},{$set: {"details.model":"14Q2"}});

//update multiple documents. (multi:true)
db.items.update({item: "Pen"},{$set:{catogory:"stationary"},$currentDate:{lastModified:true}},{multi:true});


4. MongoDB : Installation steps

4. MongoDB : Installation steps


//check DB ranking in market
###############################################################
https://db-engines.com/en/ranking

Install MangoDB (Install MangoDB needs - 30GB and few GB of RAM.)
###############################################################
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/
-> 3rd link -> https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/#install-mdb-edition
-> MongoDB Download Center.
-> "Community Server" tab

https://www.mongodb.com/download-center?_ga=2.49479651.473795272.1532242152-1894495752.1532242152#community

###############################################################

First shell

-- Navigate to C:\Program Files\MongoDB\Server\4.0\bin\
-- mongod.exe --dbpath D:\mongoDBDatabase --rest --journal  // --replSet (Personal database path) 

Second shell

-- Navigate to C:\Program Files\MongoDB\Server\4.0\bin\
-- mongo.exe






3. MongoDB : NO SQL databases types

3. MongoDB : NO SQL databases types