Sunday, September 30, 2018

MONGODB Aggregation Operations

-- TO VIEW THE DOCUMENT: ALL COLUMNS 

> db.doccollect.find({likes:100});
{ "_id" : ObjectId("5b4abeec47c637ee06e1cbc9"), "title" : "MongoDB doc100", "description" : "MongoDB document 1", "by" : "ashok", "tags" : [  "mongodb",  "NoSQLDB",  "DocDB",  "database" ], "likes" : 100 }
{ "_id" : ObjectId("5b4abfdf47c637ee06e1cbca"), "title" : "MongoDB doc100", "description" : "MongoDB document 1", "by" : "ashok", "tags" : [  "mongodb",  "NoSQLDB",  "DocDB",  "database" ], "likes" : 100 }
{ "_id" : ObjectId("5b4ac1f347c637ee06e1cbcb"), "title" : "MongoDB doc100", "description" : "MongoDB document 1", "by" : "ashok", "tags" : [  "mongodb",  "NoSQLDB",  "DocDB",  "database" ], "likes" : 100 }
{ "_id" : ObjectId("5b4ac2d81df2d6fb5e3de700"), "title" : "casandra doc1", "description" : "NoSQLDB", "by" : "Ashok", "tags" : [  "NoSQL",  "DocDB",  "NotOnlySQL" ], "likes" : 100 }
> > db.doccollect.find().pretty();

-- ALL COLUMNS WITHOUT _ID COLUMN

> db.doccollect.find({likes:100},{_id:0});
{ "title" : "MongoDB doc100", "description" : "MongoDB document 1", "by" : "ashok", "tags" : [  "mongodb",  "NoSQLDB",  "DocDB",  "database" ], "likes" : 100 }
{ "title" : "MongoDB doc100", "description" : "MongoDB document 1", "by" : "ashok", "tags" : [  "mongodb",  "NoSQLDB",  "DocDB",  "database" ], "likes" : 100 }
{ "title" : "MongoDB doc100", "description" : "MongoDB document 1", "by" : "ashok", "tags" : [  "mongodb",  "NoSQLDB",  "DocDB",  "database" ], "likes" : 100 }
{ "title" : "casandra doc1", "description" : "NoSQLDB", "by" : "Ashok", "tags" : [  "NoSQL",  "DocDB",  "NotOnlySQL" ], "likes" : 100 }
>
>> db.doccollect.find({like:100},{_id:1});

> db.doccollect.find({likes:100});
{ "_id" : ObjectId("5b4abeec47c637ee06e1cbc9"), "title" : "MongoDB doc100", "description" : "MongoDB document 1", "by" : "ashok", "tags" : [  "mongodb",  "NoSQLDB",  "DocDB",  "database" ], "likes" : 100 }
{ "_id" : ObjectId("5b4abfdf47c637ee06e1cbca"), "title" : "MongoDB doc100", "description" : "MongoDB document 1", "by" : "ashok", "tags" : [  "mongodb",  "NoSQLDB",  "DocDB",  "database" ], "likes" : 100 }
{ "_id" : ObjectId("5b4ac1f347c637ee06e1cbcb"), "title" : "MongoDB doc100", "description" : "MongoDB document 1", "by" : "ashok", "tags" : [  "mongodb",  "NoSQLDB",  "DocDB",  "database" ], "likes" : 100 }
{ "_id" : ObjectId("5b4ac2d81df2d6fb5e3de700"), "title" : "casandra doc1", "description" : "NoSQLDB", "by" : "Ashok", "tags" : [  "NoSQL",  "DocDB",  "NotOnlySQL" ], "likes" : 100 }
>

-- TO VIEW THE DOCUMENT: ONLY _id COLUMN

>
> db.doccollect.find({likes:80},{_id:1});
{ "_id" : ObjectId("5b4acd8b1df2d6fb5e3de78d") }
>
> db.doccollect.find({likes:100},{_id:1});
{ "_id" : ObjectId("5b4abeec47c637ee06e1cbc9") }
{ "_id" : ObjectId("5b4abfdf47c637ee06e1cbca") }
{ "_id" : ObjectId("5b4ac1f347c637ee06e1cbcb") }
{ "_id" : ObjectId("5b4ac2d81df2d6fb5e3de700") }
>


-- TO VIEW THE DOCUMENTS: ONE COLUMN WITHOUT _id column

> db.doccollect.find({likes:80},{_id:0,title:1});
{ "title" : "MongoDB doc10" }
>
>
> db.doccollect.find({likes:100},{_id:0,title:1});
{ "title" : "MongoDB doc100" }
{ "title" : "MongoDB doc100" }
{ "title" : "MongoDB doc100" }
{ "title" : "casandra doc1" }
>


-- TO VIEW THE DOCUMENT: MULTIPLE COLUMNS WITHOUT _id column

> db.doccollect.find({likes:100},{_id:0,title:1,by: "ashok", likes:1});
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "casandra doc1", "by" : "Ashok", "likes" : 100 }
>

> db.doccollect.find({likes:80}, {_id:0,title:1, by: "Ashok",title:1});
{ "title" : "MongoDB doc10", "by" : "Ashok" }
>
>


> db.doccollect.find({likes:80},{_id:1,title:1});
{ "_id" : ObjectId("5b4c8f965d075b50580db385"), "title" : "PostGresql doc1" }
>


> db.doccollect.find({likes:80},{_id:1,title:1,tags:1});
{ "_id" : ObjectId("5b4c8f965d075b50580db385"), "title" : "PostGresql doc1", "tags" : [  "Postgresql",  "rdbmsdoc",  "nosql" ] }
>

#CREATE INVENTORY COLLECTION 


# INSERT items in inventory collection

> db.inventory.insert({ item: "canvas", qty: 100, tags: ["cotton"], size: { h: 28, w: 35.5, uom: "cm" } });


-- Find all items in inventory collection

> db.inventory.find();


> db.inventory.insert([
...  { item: "journal", qty: 25, tags: ["blank", "red"], size: { h: 14, w: 21, uom: "cm" } },
... { item: "mat", qty: 85, tags: ["gray"], size: { h: 27.9, w: 35.5, uom: "cm" } },
... { item: "mousepad", qty: 25, tags: ["gel", "blue"], size: { h: 19, w: 22.85, uom: "cm" } }
... ]);


> db.inventory.find();
{ "_id" : ObjectId("5b4c96325d075b50580db386"), "item" : "canvas", "qty" : 100, "tags" : [  "cotton" ], "size" : { "h" : 28, "w" : 35.5, "uom" : "cm" } }
{ "_id" : ObjectId("5b4c970b5d075b50580db387"), "item" : "journal", "qty" : 25, "tags" : [  "blank",  "red" ], "size" : { "h" : 14, "w" : 21, "uom" : "cm" } }
{ "_id" : ObjectId("5b4c970b5d075b50580db388"), "item" : "mat", "qty" : 85, "tags" : [  "gray" ], "size" : { "h" : 27.9, "w" : 35.5, "uom" : "cm" } }
{ "_id" : ObjectId("5b4c970b5d075b50580db389"), "item" : "mousepad", "qty" : 25, "tags" : [  "gel",  "blue" ], "size" : { "h" : 19, "w" : 22.85, "uom" : "cm" } }
>


-- TO VIEW THE DOCUMENT: EQUAL TO

-- USING CHARACTER

> db.doccollect.find({"by" : "ashok"},{by:1,title:1,_id:0});
{ "title" : "MongoDB doc100", "by" : "ashok" }
{ "title" : "MongoDB doc100", "by" : "ashok" }
{ "title" : "MongoDB doc100", "by" : "ashok" }
>
>
>
>
>
> db.doccollect.find({"by" : "Ashok"},{by:1,title:1,_id:0});
{ "title" : "casandra doc1", "by" : "Ashok" }
{ "title" : "MongoDB doc10", "by" : "Ashok" }
>


-- USING NUMBER

> db.doccollect.find({likes:80},{by:1,title:1,_id:0,likes:1});]

{ "title" : "MongoDB doc10", "by" : "Ashok", "likes" : 80 }
>
>
>
>
> db.doccollect.find({likes:100},{by:1,title:1,_id:0,likes:1});
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "casandra doc1", "by" : "Ashok", "likes" : 100 }


-- USING CHARACTER - not equal to 

> db.doccollect.find({by: {$ne: "ashok"}}, {by:1,title:1,_id:0});

{ "title" : "casandra doc1", "by" : "Ashok" }
{ "title" : "MongoDB doc10", "by" : "Ashok" }
{ "title" : "Artificial Inteligence", "by" : "Siva" }
>



-- USING NUMBER -- NOT EQUAL TO -- $ne


db.doccollect.find({likes: {$ne: 100}}, {by:1,title:1,_id:0});


>
> db.doccollect.find({likes: {$ne: 100}}, {by:1,title:1,_id:0});

{ "title" : "MongoDB doc10", "by" : "Ashok" }
{ "title" : "Artificial Inteligence", "by" : "Siva" }
>
>



> db.doccollect.find({likes:{$ne:80}},{by:1,title:1,_id:0});

{ "title" : "MongoDB doc100", "by" : "ashok" }
{ "title" : "MongoDB doc100", "by" : "ashok" }
{ "title" : "MongoDB doc100", "by" : "ashok" }
{ "title" : "casandra doc1", "by" : "Ashok" }
{ "title" : "Artificial Inteligence", "by" : "Siva" }
>
>
>

> db.users10.find({"Age":{$ne : 30}}, {Name:1,Age:1,Gender:1,_id:0});

{ "Name" : " Test User1", "Age" : 11, "Gender" : "F" }
{ "Name" : " Test User2", "Age" : 12, "Gender" : "F" }
{ "Name" : " Test User3", "Age" : 13, "Gender" : "F" }
{ "Name" : " Test User4", "Age" : 14, "Gender" : "F" }
{ "Name" : " Test User5", "Age" : 15, "Gender" : "F" }
{ "Name" : " Test User6", "Age" : 16, "Gender" : "F" }
{ "Name" : " Test User7", "Age" : 17, "Gender" : "F" }
{ "Name" : " Test User8", "Age" : 18, "Gender" : "F" }
{ "Name" : " Test User9", "Age" : 19, "Gender" : "F" }
{ "Name" : " Test User10", "Age" : 20, "Gender" : "F" }
{ "Name" : " Test User11", "Age" : 21, "Gender" : "F" }
{ "Name" : " Test User12", "Age" : 22, "Gender" : "F" }
{ "Name" : " Test User13", "Age" : 23, "Gender" : "F" }
{ "Name" : " Test User14", "Age" : 24, "Gender" : "F" }
{ "Name" : " Test User15", "Age" : 25, "Gender" : "F" }
{ "Name" : " Test User16", "Age" : 26, "Gender" : "F" }
{ "Name" : " Test User17", "Age" : 27, "Gender" : "F" }
{ "Name" : " Test User18", "Age" : 28, "Gender" : "F" }
{ "Name" : " Test User19", "Age" : 29, "Gender" : "F" }
{ "Name" : " Test User1", "Age" : 11, "Gender" : "F" }
Type "it" for more
>


> db.users10.find({Name : {$ne : " Test User20"}}, {Name:1,Age:1,Gender:1,_id:0});

{ "Name" : " Test User1", "Age" : 11, "Gender" : "F" }
{ "Name" : " Test User2", "Age" : 12, "Gender" : "F" }
{ "Name" : " Test User3", "Age" : 13, "Gender" : "F" }
{ "Name" : " Test User4", "Age" : 14, "Gender" : "F" }
{ "Name" : " Test User5", "Age" : 15, "Gender" : "F" }
{ "Name" : " Test User6", "Age" : 16, "Gender" : "F" }
{ "Name" : " Test User7", "Age" : 17, "Gender" : "F" }
{ "Name" : " Test User8", "Age" : 18, "Gender" : "F" }
{ "Name" : " Test User9", "Age" : 19, "Gender" : "F" }
{ "Name" : " Test User10", "Age" : 20, "Gender" : "F" }
{ "Name" : " Test User11", "Age" : 21, "Gender" : "F" }
{ "Name" : " Test User12", "Age" : 22, "Gender" : "F" }
{ "Name" : " Test User13", "Age" : 23, "Gender" : "F" }
{ "Name" : " Test User14", "Age" : 24, "Gender" : "F" }
{ "Name" : " Test User15", "Age" : 25, "Gender" : "F" }
{ "Name" : " Test User16", "Age" : 26, "Gender" : "F" }
{ "Name" : " Test User17", "Age" : 27, "Gender" : "F" }
{ "Name" : " Test User18", "Age" : 28, "Gender" : "F" }
{ "Name" : " Test User19", "Age" : 29, "Gender" : "F" }
{ "Name" : " Test User1", "Age" : 11, "Gender" : "F" }
Type "it" for more

------GREATER THAN 

> db.doccollect.find({likes: {$gt :80}}, {by:1,title:1,_id:0,likes:1});

{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "casandra doc1", "by" : "Ashok", "likes" : 100 }
>



> db.users10.find({Age:{$gt :50}}, {Name:1,Age:1,Gender:1,_id:0});

{ "Name" : " Test User41", "Age" : 51, "Gender" : "F" }
{ "Name" : " Test User42", "Age" : 52, "Gender" : "F" }
{ "Name" : " Test User43", "Age" : 53, "Gender" : "F" }
{ "Name" : " Test User44", "Age" : 54, "Gender" : "F" }
{ "Name" : " Test User45", "Age" : 55, "Gender" : "F" }
{ "Name" : " Test User46", "Age" : 56, "Gender" : "F" }
{ "Name" : " Test User47", "Age" : 57, "Gender" : "F" }
{ "Name" : " Test User48", "Age" : 58, "Gender" : "F" }
{ "Name" : " Test User49", "Age" : 59, "Gender" : "F" }
{ "Name" : " Test User50", "Age" : 60, "Gender" : "F" }
{ "Name" : " Test User51", "Age" : 61, "Gender" : "F" }
{ "Name" : " Test User52", "Age" : 62, "Gender" : "F" }
{ "Name" : " Test User53", "Age" : 63, "Gender" : "F" }
{ "Name" : " Test User54", "Age" : 64, "Gender" : "F" }
{ "Name" : " Test User55", "Age" : 65, "Gender" : "F" }
{ "Name" : " Test User56", "Age" : 66, "Gender" : "F" }
{ "Name" : " Test User57", "Age" : 67, "Gender" : "F" }
{ "Name" : " Test User58", "Age" : 68, "Gender" : "F" }
{ "Name" : " Test User59", "Age" : 69, "Gender" : "F" }
{ "Name" : " Test User60", "Age" : 70, "Gender" : "F" }



-- GREATHER THAN OR EUAL TO 

> db.doccollect.find({likes:{$gte:80}}, {by:1,title:1,_id:0,likes:1});
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "casandra doc1", "by" : "Ashok", "likes" : 100 }
{ "title" : "MongoDB doc10", "by" : "Ashok", "likes" : 80 }
>
>
>
> db.doccollect.find({likes:{$gte:100}}, {by:1,title:1,_id:0,likes:1});

{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "casandra doc1", "by" : "Ashok", "likes" : 100 }
>

-- LESS THAN 

>  db.doccollect.find({likes:{$lt:100}}, {by:1,title:1,_id:0,likes:1});

{ "title" : "MongoDB doc10", "by" : "Ashok", "likes" : 80 }
>

> db.users10.find({Age:{$lt:50}},{Age:1,Gender:1,Country:1,_id:0});

{ "Age" : 11, "Gender" : "F", "Country" : "U.K." }
{ "Age" : 12, "Gender" : "F", "Country" : "U.K." }
{ "Age" : 13, "Gender" : "F", "Country" : "U.K." }
{ "Age" : 14, "Gender" : "F", "Country" : "U.K." }
{ "Age" : 15, "Gender" : "F", "Country" : "U.K." }
-
-

{ "Age" : 30, "Gender" : "F", "Country" : "U.K." }
-
-
-
-
-
{ "Age" : 40, "Gender" : "F", "Country" : "U.K." }
-
-
-

{ "Age" : 48, "Gender" : "F", "Country" : "U.K." }
{ "Age" : 49, "Gender" : "F", "Country" : "U.K." }
> it
no cursor
>


-- LESSER THAN OR EQUAL TO 

db.users10.find({Age:{$lte:50}},{Age:1,Gender:1,Country:1,_id:0});

> db.users10.find({Age:{$lt:50}},{Age:1,Gender:1,Country:1,_id:0});
{ "Age" : 11, "Gender" : "F", "Country" : "U.K." }
{ "Age" : 12, "Gender" : "F", "Country" : "U.K." }
{ "Age" : 13, "Gender" : "F", "Country" : "U.K." }
{ "Age" : 14, "Gender" : "F", "Country" : "U.K." }

-

{ "Age" : 47, "Gender" : "F", "Country" : "U.K." }
{ "Age" : 48, "Gender" : "F", "Country" : "U.K." }
{ "Age" : 49, "Gender" : "F", "Country" : "U.K." }
{ "Age" : 50, "Gender" : "F", "Country" : "U.K." }
>

db.doccollect.find({likes:{$lte:80}}, {by:1,_id:0,});


> db.doccollect.find({likes:{$lte:80}}, {by:1,_id:0,});
{ "by" : "Ashok" }
>

>
> db.doccollect.find({likes:{$lte:100}}, {by:1,_id:0,});
{ "by" : "ashok" }
{ "by" : "ashok" }
{ "by" : "ashok" }
{ "by" : "Ashok" }
{ "by" : "Ashok" }
>



---######################### QUERY OPERATOR#################----------------

--- AND OPERATOR

> db.doccollect.find({by: 'Ashok',likes:100}, {by:1,title:1,_id:0,likes:1})

{ "title" : "casandra doc1", "by" : "Ashok", "likes" : 100 }
>
> db.doccollect.find({by: 'Ashok',likes:80}, {by:1,title:1,_id:0,likes:1})
{ "title" : "MongoDB doc10", "by" : "Ashok", "likes" : 80 }
>

db.createCollection("inventory");

db.inventory.insert(
{"item" : "abc1", description: "product 1", qty: 300 }
{"item" : "abc2", description: "product 2", qty: 200 }
{"item" : "xyz1", description: "product 3", qty: 250 }
{"item" : "VWZ1", description: "product 4", qty: 300 }
{"item" : "VWZ2", description: "product 5", qty: 180 }

--################################################################################################
-- Create inventory collection 
--################################################################################################

db.inventory.insert({ item: "canvas", qty: 100, tags: ["cotton"], size: { h: 28, w: 35.5, uom: "cm" } });
> db.inventory.insert({ item: "kungfo", qty: 100, tags: ["lether"], size: { h:32, w: 25.5, uom: "mm" } });
>
> db.inventory.insert({ item: "slip-on shoe", qty: 100, tags: ["casual"], size: { h:30, w: 25.5, uom: "cm" } });
>
> db.inventory.insert([{ item: "slip-on shoe", qty: 100, tags: ["casual"], size: { h:30, w: 25.5, uom: "cm" } },
... { item: "Jelly shoes", qty:50,  tags: ["casual"], size: { h:28, w: 25.5, uom: "mm"}}]);

>
> db.inventory.find({qty:100, item:'canvas'});

{ "_id" : ObjectId("5b4d8202a45df39b8bfc6a68"), "item" : "canvas", "qty" : 100, "tags" : [  "cotton" ], "size" : { "h" : 28, "w" : 35.5, "uom" : "cm" } }
>
>
> db.inventory.find({qty:100, item:'canvas'},{qty:1,item:1,_id:0});

{ "item" : "canvas", "qty" : 100 }
>

-- CREATE student collection in ashokdb database 

-- insert below documents in collection 

db.student.insert(
[
        {
                "f_name" : "Zenny",
                "sex" : "Female",
                "class" : "VI",
                "age" : 12,
                "grd_point" : 32.6342
        },
        {
                "f_name" : "Paul",
                "sex" : "Male",
                "class" : "VII",
                "age" : 13,
                "grd_point" : 29.5904
        },
        {
                "f_name" : "Tom",
                "sex" : "Male",
                "class" : "VI",
                "age" : 11,
                "grd_point" : 30.1257
        },
        {
                "f_name" : "Lassy",
                "sex" : "Female",
                "class" : "VIII",
                "age" : 13,
                "grd_point" : 28.2514
        },
        {
                "f_name" : "Peter",
                "sex" : "Male",
                "class" : "VI",
                "age" : 11,
                "grd_point" : 31.5201
        }
]
);

--################################################################################################
1) how to join the collection;
2)how to merge two collection 
3) how to join the two documents by using AND operators
--################################################################################################


db.student.find({$and:[{"sex":"Male"},{"grd_point":{ $gte: 31 }},{"class":"VI"}]}).pretty();


> db.student.find();
{ "_id" : ObjectId("5b53228f6639d8ab61a1327e"), "f_name" : "Zenny", "sex" : "Female", "class" : "VI", "age" : 12, "grd_point" : 32.6342 }
{ "_id" : ObjectId("5b53228f6639d8ab61a1327f"), "f_name" : "Paul", "sex" : "Male", "class" : "VII", "age" : 13, "grd_point" : 29.5904 }
{ "_id" : ObjectId("5b53228f6639d8ab61a13280"), "f_name" : "Tom", "sex" : "Male", "class" : "VI", "age" : 11, "grd_point" : 30.1257 }
{ "_id" : ObjectId("5b53228f6639d8ab61a13281"), "f_name" : "Lassy", "sex" : "Female", "class" : "VIII", "age" : 13, "grd_point" : 28.2514 }
{ "_id" : ObjectId("5b53228f6639d8ab61a13282"), "f_name" : "Peter", "sex" : "Male", "class" : "VI", "age" : 11, "grd_point" : 31.5201 }
>
>
>
-- In the below condition describe as sex=male and grade_point < 31 and class=VI 

> db.student.find({$and:[{"sex":"Male"},{"grd_point":{ $gte: 31 }},{"class":"VI"}]}).pretty();
{
        "_id" : ObjectId("5b53228f6639d8ab61a13282"),
        "f_name" : "Peter",
        "sex" : "Male",
        "class" : "VI",
        "age" : 11,
        "grd_point" : 31.5201
}
>

> db.student.find({$and:[{"sex":"Male"},{"grd_point":{ $gte: 31 }},{"class":"VI"}]});

{ "_id" : ObjectId("5b53228f6639d8ab61a13282"), "f_name" : "Peter", "sex" : "Male", "class" : "VI", "age" : 11, "grd_point" : 31.5201 }
>
>
>


/// Example: SQL LIKE  SELECT *  FROM student  WHERE sex="Male" AND grd_point>=31 AND class="VI"; ///



-- TO VIEW THE DOCUMENT: OR operator

-- first see all records 

> db.doccollect.find();
{ "_id" : ObjectId("5b4abeec47c637ee06e1cbc9"), "title" : "MongoDB doc100", "description" : "MongoDB document 1", "by" : "ashok", "tags" : [  "mongodb",  "NoSQLDB",  "DocDB",  "database" ], "likes" : 100 }
{ "_id" : ObjectId("5b4abfdf47c637ee06e1cbca"), "title" : "MongoDB doc100", "description" : "MongoDB document 1", "by" : "ashok", "tags" : [  "mongodb",  "NoSQLDB",  "DocDB",  "database" ], "likes" : 100 }
{ "_id" : ObjectId("5b4ac1f347c637ee06e1cbcb"), "title" : "MongoDB doc100", "description" : "MongoDB document 1", "by" : "ashok", "tags" : [  "mongodb",  "NoSQLDB",  "DocDB",  "database" ], "likes" : 100 }
{ "_id" : ObjectId("5b4ac2d81df2d6fb5e3de700"), "title" : "casandra doc1", "description" : "NoSQLDB", "by" : "Ashok", "tags" : [  "NoSQL",  "DocDB",  "NotOnlySQL" ], "likes" : 100 }
{ "_id" : ObjectId("5b4acd8b1df2d6fb5e3de78d"), "title" : "MongoDB doc10", "description" : "MongoDB document 10", "by" : "Ashok", "tags" : [  "MongoDB",  "NoSQLDB",  "DocDB",  "database" ], "likes" : 80 }
{ "_id" : ObjectId("5b4ad2851df2d6fb5e3de790"), "title" : "Artificial Inteligence", "by" : "Siva" }
>

> db.doccollect.find({$or:[{by: "Siva"},{likes:80}]},{by:1,_id:0,title:1,description:1,likes:1});
{ "title" : "MongoDB doc10", "description" : "MongoDB document 10", "by" : "Ashok", "likes" : 80 }
{ "title" : "Artificial Inteligence", "by" : "Siva" }
>


-- @@@@@@@@@@@@@@@@@@    OR operator and Greater Than @@@@@@@@@@@@@@@@@@@

> db.doccollect.find();

{ "_id" : ObjectId("5b4abeec47c637ee06e1cbc9"), "title" : "MongoDB doc100", "description" : "MongoDB document 1", "by" : "ashok", "tags" : [  "mongodb",  "NoSQLDB",  "DocDB",  "database" ], "likes" : 100 }
{ "_id" : ObjectId("5b4abfdf47c637ee06e1cbca"), "title" : "MongoDB doc100", "description" : "MongoDB document 1", "by" : "ashok", "tags" : [  "mongodb",  "NoSQLDB",  "DocDB",  "database" ], "likes" : 100 }
{ "_id" : ObjectId("5b4ac1f347c637ee06e1cbcb"), "title" : "MongoDB doc100", "description" : "MongoDB document 1", "by" : "ashok", "tags" : [  "mongodb",  "NoSQLDB",  "DocDB",  "database" ], "likes" : 100 }
{ "_id" : ObjectId("5b4ac2d81df2d6fb5e3de700"), "title" : "casandra doc1", "description" : "NoSQLDB", "by" : "Ashok", "tags" : [  "NoSQL",  "DocDB",  "NotOnlySQL" ], "likes" : 100 }
{ "_id" : ObjectId("5b4acd8b1df2d6fb5e3de78d"), "title" : "MongoDB doc10", "description" : "MongoDB document 10", "by" : "Ashok", "tags" : [  "MongoDB",  "NoSQLDB",  "DocDB",  "database" ], "likes" : 80 }
{ "_id" : ObjectId("5b4ad2851df2d6fb5e3de790"), "title" : "Artificial Inteligence", "by" : "Siva" }
>

db.doccollect.insert( {"title" : "MongoDB doc50", "description" : "PostgresQL DB", "by" : "Kamal", 
              "tags" : [  "Postgresql",  "RdbmsSQLDB",  "DocDB",  "database" ], "likes" : 80})
{

> db.doccollect.find({$or:[{by:"Kamal"},{likes:{$gt:100}}]},{by:1,title:1,_id:0,likes:1});
{ "title" : "MongoDB doc50", "by" : "Kamal", "likes" : 80 }
>

> db.doccollect.find({$or:[{by:'ashok'},{likes:{$gt:80}}]},{by:1,title:1,_id:0,likes:1});
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "casandra doc1", "by" : "Ashok", "likes" : 100 }
>
>

> db.doccollect.find({$or:[{by:'ashok'},{likes:{$gt:100}}]},{by:1,title:1,_id:0,likes:1});
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
>


> db.doccollect.find({$or:[{by:'Ashok'},{likes:{$gt:80}}]},{by:1,title:1,_id:0,likes:1});
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "casandra doc1", "by" : "Ashok", "likes" : 100 }
{ "title" : "MongoDB doc10", "by" : "Ashok", "likes" : 80 }    ------->>> here by:Ashok is matching
>

-- @@@@@@@@@@@@@@@@@@    NOT OR operator and Greater Than @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

> db.doccollect.find({$nor:[{by:"Kamal"},{likes:{$gt:100}}]},{by:1,title:1,_id:0,likes:1});
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "casandra doc1", "by" : "Ashok", "likes" : 100 }
{ "title" : "MongoDB doc10", "by" : "Ashok", "likes" : 80 }
{ "title" : "Artificial Inteligence", "by" : "Siva" }
>

> db.doccollect.find({$nor:[{by:"ashok"},{likes:{$gt:100}}]},{by:1,title:1,_id:0,likes:1});
{ "title" : "casandra doc1", "by" : "Ashok", "likes" : 100 }
{ "title" : "MongoDB doc10", "by" : "Ashok", "likes" : 80 }
{ "title" : "Artificial Inteligence", "by" : "Siva" }
{ "title" : "MongoDB doc50", "by" : "Kamal", "likes" : 80 }
>
>
> db.doccollect.find({$nor:[{by:'Ashok'},{likes:{$gt:80}}]},{by:1,title:1,_id:0,likes:1});
{ "title" : "Artificial Inteligence", "by" : "Siva" }
{ "title" : "MongoDB doc50", "by" : "Kamal", "likes" : 80 }   ---->>> here not equal to (by:ashok) is matching 
>

>
>

-- @@@@@@@@@@@@@@@@@@   AND and OR OPERATOR @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

>
> db.doccollect.find({by:'Ashok',$or:[{title:' MongoDB doc100'}, {likes:{$gt:80}}]},{by:1,title:1,_id:0,likes:1})

{ "title" : "casandra doc1", "by" : "Ashok", "likes" : 100 }
>

>
> db.doccollect.find({by:'Ashok',$or:[{title:' MongoDB doc100'}, {likes:{$gt:100}}]},{by:1,title:1,_id:0,likes:1})
>


> db.doccollect.find({by:'Kamal',$or:[{title:' MongoDB doc100'}, {likes:{$gt:50}}]},{by:1,title:1,_id:0,likes:1})
{ "title" : "MongoDB doc50", "by" : "Kamal", "likes" : 80 }
>
>
>
>
db.doccollect.insert( {"title" : "MongoDB DOC 120", "description" : "SAP HANA db", "by" : "Ankur", 
              "tags" : [  "SAP-Hana",  "RdbmsSQLDB",  "binary",  "database" ], "likes" : 120})

> db.doccollect.find();
{ "_id" : ObjectId("5b4abeec47c637ee06e1cbc9"), "title" : "MongoDB doc100", "description" : "MongoDB document 1", "by" : "ashok", "tags" : [  "mongodb",  "NoSQLDB",  "DocDB",  "database" ], "likes" : 100 }
{ "_id" : ObjectId("5b4abfdf47c637ee06e1cbca"), "title" : "MongoDB doc100", "description" : "MongoDB document 1", "by" : "ashok", "tags" : [  "mongodb",  "NoSQLDB",  "DocDB",  "database" ], "likes" : 100 }
{ "_id" : ObjectId("5b4ac1f347c637ee06e1cbcb"), "title" : "MongoDB doc100", "description" : "MongoDB document 1", "by" : "ashok", "tags" : [  "mongodb",  "NoSQLDB",  "DocDB",  "database" ], "likes" : 100 }
{ "_id" : ObjectId("5b4ac2d81df2d6fb5e3de700"), "title" : "casandra doc1", "description" : "NoSQLDB", "by" : "Ashok", "tags" : [  "NoSQL",  "DocDB",  "NotOnlySQL" ], "likes" : 100 }
{ "_id" : ObjectId("5b4acd8b1df2d6fb5e3de78d"), "title" : "MongoDB doc10", "description" : "MongoDB document 10", "by" : "Ashok", "tags" : [  "MongoDB",  "NoSQLDB",  "DocDB",  "database" ], "likes" : 80 }
{ "_id" : ObjectId("5b4ad2851df2d6fb5e3de790"), "title" : "Artificial Inteligence", "by" : "Siva" }
{ "_id" : ObjectId("5b534dacb1e34bc0841af025"), "title" : "MongoDB doc50", "description" : "PostgresQL DB", "by" : "Kamal", "tags" : [  "Postgresql",  "RdbmsSQLDB",  "DocDB",  "database" ], "likes" : 80 }
{ "_id" : ObjectId("5b5353c9ec36bd9344ade14e"), "title" : "MongoDB DOC 120", "description" : "SAP HANA db", "by" : "Ankur", "tags" : [  "SAP-Hana",  "RdbmsSQLDB",  "binary",  "database" ], "likes" : 120 }
>
>
--------------------------- AND and OR OPERATOR -------------------------------
>
> db.users10.find({"Age":30, $or:[{"Name" : "Test User10"},{Age:{$gt:10}}]},{_id:0,Name:1,Age:1,Gender:1,Country:1});
{ "Name" : " Test User20", "Age" : 30, "Gender" : "F", "Country" : "U.K." }
{ "Name" : " Test User20", "Age" : 30, "Gender" : "F", "Country" : "U.K." }    ---> we have 2 documents in users10 collections

>> db.users10.find({"Age":30, $or:[{"Name" : "Test User10"},{Age:{$gt:30}}]},{_id:0,Name:1,Age:1,Gender:1,Country:1});
>
> db.users10.find({"Age":30, $or:[{"Name" : "Test User10"},{Age:{$gt:20}}]},{_id:0,Name:1,Age:1,Gender:1,Country:1});
{ "Name" : " Test User20", "Age" : 30, "Gender" : "F", "Country" : "U.K." }
{ "Name" : " Test User20", "Age" : 30, "Gender" : "F", "Country" : "U.K." }
>
> db.users10.find({"Age":30, $or:[{"Name" : "Test User10"},{Age:{$gt:10}}]},{_id:0,Name:1,Age:1,Gender:1,Country:1});
{ "Name" : " Test User20", "Age" : 30, "Gender" : "F", "Country" : "U.K." }
{ "Name" : " Test User20", "Age" : 30, "Gender" : "F", "Country" : "U.K." }

>
> db.users10.find({"Age":30, $or:[{"Name" : "Test User40"},{Age:{$gt:40}}]},{_id:0,Name:1,Age:1,Gender:1,Country:1});
>

>
> db.users10.find({"Age":30, $or:[{"Name" : "Test User31"},{Age:{$gt:30}}]},{_id:0,Name:1,Age:1,Gender:1,Country:1});
>
> db.users10.find({"Age":30, $or:[{"Name" : "Test User12"},{Age:{$gt:10}}]},{_id:0,Name:1,Age:1,Gender:1,Country:1});
{ "Name" : " Test User20", "Age" : 30, "Gender" : "F", "Country" : "U.K." }
{ "Name" : " Test User20", "Age" : 30, "Gender" : "F", "Country" : "U.K." }
>

--


------ BETWEEN 

> db.doccollect.find({likes:{$gt:50,$lte:100}},{_id:0,title:1,by:1,likes:1});
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "casandra doc1", "by" : "Ashok", "likes" : 100 }
{ "title" : "MongoDB doc10", "by" : "Ashok", "likes" : 80 }
{ "title" : "MongoDB doc50", "by" : "Kamal", "likes" : 80 }
>

> db.doccollect.find({likes:{$gt:80,$lte:120}},{_id:0,title:1,by:1,likes:1});
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "casandra doc1", "by" : "Ashok", "likes" : 100 }
{ "title" : "MongoDB DOC 120", "by" : "Ankur", "likes" : 120 }
>


> db.doccollect.find({likes:{$gt:50,$lt:120}},{_id:0,title:1,by:1,likes:1});
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "casandra doc1", "by" : "Ashok", "likes" : 100 }
{ "title" : "MongoDB doc10", "by" : "Ashok", "likes" : 80 }
{ "title" : "MongoDB doc50", "by" : "Kamal", "likes" : 80 }

> db.doccollect.find({likes:{$gt:80,$lt:120}},{_id:0,title:1,by:1,likes:1});
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "casandra doc1", "by" : "Ashok", "likes" : 100 }
>
--@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ IN OPERATOR @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

> db.doccollect.find({likes:{$in:[50,80,100]}},{_id:0,title:1,by:1,likes:1});

{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "casandra doc1", "by" : "Ashok", "likes" : 100 }
{ "title" : "MongoDB doc10", "by" : "Ashok", "likes" : 80 }
{ "title" : "MongoDB doc50", "by" : "Kamal", "likes" : 80 }
>
>
>
>
> db.doccollect.find({likes:{$in:[80,100]}},{_id:0,title:1,by:1,likes:1});

{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "casandra doc1", "by" : "Ashok", "likes" : 100 }
{ "title" : "MongoDB doc10", "by" : "Ashok", "likes" : 80 }
{ "title" : "MongoDB doc50", "by" : "Kamal", "likes" : 80 }
>


--@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ NOT IN @@@@@@@@@@@@@@@@@@@@@@@@@@@@@


> db.doccollect.find({likes:{$nin:[80,100]}},{_id:0,title:1,by:1,likes:1});

{ "title" : "Artificial Inteligence", "by" : "Siva" }
{ "title" : "MongoDB DOC 120", "by" : "Ankur", "likes" : 120 }
>


> db.doccollect.find({likes:{$nin:[80]}},{_id:0,title:1,by:1,likes:1});
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "casandra doc1", "by" : "Ashok", "likes" : 100 }
{ "title" : "Artificial Inteligence", "by" : "Siva" }
{ "title" : "MongoDB DOC 120", "by" : "Ankur", "likes" : 120 }
>


--@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ NULL @@@@@@@@@@@@@@@@@@@@@@@@@@@@@


> db.doccollect.find({likes:null},{_id:0,title:1,by:1,likes:1});

{ "title" : "Artificial Inteligence", "by" : "Siva" }
>
>
>

> db.users10.find({Country:{$ne:null}},{_id:0,Name:1,Age:1,Gender:1});

{ "Name" : " Test User1", "Age" : 11, "Gender" : "F" }
{ "Name" : " Test User2", "Age" : 12, "Gender" : "F" }
{ "Name" : " Test User3", "Age" : 13, "Gender" : "F" }
{ "Name" : " Test User4", "Age" : 14, "Gender" : "F" }
{ "Name" : " Test User5", "Age" : 15, "Gender" : "F" }
{ "Name" : " Test User6", "Age" : 16, "Gender" : "F" }
{ "Name" : " Test User7", "Age" : 17, "Gender" : "F" }
{ "Name" : " Test User8", "Age" : 18, "Gender" : "F" }
{ "Name" : " Test User9", "Age" : 19, "Gender" : "F" }
{ "Name" : " Test User10", "Age" : 20, "Gender" : "F" }
{ "Name" : " Test User11", "Age" : 21, "Gender" : "F" }
{ "Name" : " Test User12", "Age" : 22, "Gender" : "F" }
{ "Name" : " Test User13", "Age" : 23, "Gender" : "F" }
{ "Name" : " Test User14", "Age" : 24, "Gender" : "F" }
{ "Name" : " Test User15", "Age" : 25, "Gender" : "F" }
{ "Name" : " Test User16", "Age" : 26, "Gender" : "F" }
{ "Name" : " Test User17", "Age" : 27, "Gender" : "F" }
{ "Name" : " Test User18", "Age" : 28, "Gender" : "F" }
{ "Name" : " Test User19", "Age" : 29, "Gender" : "F" }
{ "Name" : " Test User20", "Age" : 30, "Gender" : "F" }
c

--@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ NOT NULL @@@@@@@@@@@@@@@@@@@@@@@@@@@@@

> db.doccollect.find({likes:{$ne:null}},{_id:0,title:1,by:1,likes:1});

{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "casandra doc1", "by" : "Ashok", "likes" : 100 }
{ "title" : "MongoDB doc10", "by" : "Ashok", "likes" : 80 }
{ "title" : "MongoDB doc50", "by" : "Kamal", "likes" : 80 }
{ "title" : "MongoDB DOC 120", "by" : "Ankur", "likes" : 120 }
>
>

> db.doccollect.find({likes:{$exists:true}},{_id:0,title:1,by:1,likes:1});

{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "casandra doc1", "by" : "Ashok", "likes" : 100 }
{ "title" : "MongoDB doc10", "by" : "Ashok", "likes" : 80 }
{ "title" : "MongoDB doc50", "by" : "Kamal", "likes" : 80 }
{ "title" : "MongoDB DOC 120", "by" : "Ankur", "likes" : 120 }
>
>
>

--@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ SORTING DOCUMENTS @@@@@@@@@@@@@@@@@@@@@@@@@@@@@

--@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Sorting  a document in Ascending Order condition @@@@@@@@@@@@@@@@@@@@@@@@@@@@@

> db.doccollect.find({likes:{$gt:80}}, {by:1,title:1,_id:0,likes:1}).sort({likes:1});

{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "casandra doc1", "by" : "Ashok", "likes" : 100 }
{ "title" : "MongoDB DOC 120", "by" : "Ankur", "likes" : 120 }
>


> db.doccollect.find({likes:{$lt:100}}, 
{by:1,title:1,_id:0,likes:1}).sort({likes:1});

{ "title" : "MongoDB doc10", "by" : "Ashok", "likes" : 80 }
{ "title" : "MongoDB doc50", "by" : "Kamal", "likes" : 80 }
>
>
>
>
> db.doccollect.find({likes:{$lt:120}}, {by:1,title:1,_id:0,likes:1}).sort({likes:1});

{ "title" : "MongoDB doc10", "by" : "Ashok", "likes" : 80 }
{ "title" : "MongoDB doc50", "by" : "Kamal", "likes" : 80 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "casandra doc1", "by" : "Ashok", "likes" : 100 }
>
>


--@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Sorting a document in Descending order condition @@@@@@@@@@@@@@@@@@@@@@@@@@@@@


> db.doccollect.find({likes:{$lt:120}}, {by:1,title:1,_id:0,likes:1}).sort({likes:-1});

{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "casandra doc1", "by" : "Ashok", "likes" : 100 }
{ "title" : "MongoDB doc10", "by" : "Ashok", "likes" : 80 }
{ "title" : "MongoDB doc50", "by" : "Kamal", "likes" : 80 }
>
>


> db.doccollect.find({likes:{$lt:100}}, {by:1,title:1,_id:0,likes:1}).sort({likes:-1});

{ "title" : "MongoDB doc10", "by" : "Ashok", "likes" : 80 }
{ "title" : "MongoDB doc50", "by" : "Kamal", "likes" : 80 }
>

--@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ SORT A DOCUMENT BASED ON MULTIPLE COLUMNS @@@@@@@@@@@@@@@@@@@@@@@@@@@@@

> db.doccollect.find({likes:{$lt:120}}, {by:1,title:1,_id:0,likes:1}).sort({by:1,likes:1});


{ "title" : "MongoDB doc10", "by" : "Ashok", "likes" : 80 }
{ "title" : "casandra doc1", "by" : "Ashok", "likes" : 100 }
{ "title" : "MongoDB doc50", "by" : "Kamal", "likes" : 80 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
>

> db.doccollect.find({likes:{$lt:120}}, {by:1,title:1,_id:0,likes:1}).sort({by:-1,likes:1});

{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc50", "by" : "Kamal", "likes" : 80 }
{ "title" : "MongoDB doc10", "by" : "Ashok", "likes" : 80 }
{ "title" : "casandra doc1", "by" : "Ashok", "likes" : 100 }
>

> db.doccollect.find({likes:{$lt:120}}, {by:1,title:1,_id:0,likes:1}).sort({by:-1,likes:-1});

{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc50", "by" : "Kamal", "likes" : 80 }
{ "title" : "casandra doc1", "by" : "Ashok", "likes" : 100 }
{ "title" : "MongoDB doc10", "by" : "Ashok", "likes" : 80 }
>

> db.doccollect.find({likes:{$lt:120}}, {by:1,title:1,_id:0,likes:1}).sort({by:1,likes:-1});

{ "title" : "casandra doc1", "by" : "Ashok", "likes" : 100 }
{ "title" : "MongoDB doc10", "by" : "Ashok", "likes" : 80 }
{ "title" : "MongoDB doc50", "by" : "Kamal", "likes" : 80 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
>
----------------------------------------------------------------------------------------------------------------------------------------

> db.student.find();

{ "_id" : ObjectId("5b53228f6639d8ab61a1327e"), "f_name" : "Zenny", "sex" : "Female", "class" : "VI", "age" : 12, "grd_point" : 32.6342 }
{ "_id" : ObjectId("5b53228f6639d8ab61a1327f"), "f_name" : "Paul", "sex" : "Male", "class" : "VII", "age" : 13, "grd_point" : 29.5904 }
{ "_id" : ObjectId("5b53228f6639d8ab61a13280"), "f_name" : "Tom", "sex" : "Male", "class" : "VI", "age" : 11, "grd_point" : 30.1257 }
{ "_id" : ObjectId("5b53228f6639d8ab61a13281"), "f_name" : "Lassy", "sex" : "Female", "class" : "VIII", "age" : 13, "grd_point" : 28.2514 }
{ "_id" : ObjectId("5b53228f6639d8ab61a13282"), "f_name" : "Peter", "sex" : "Male", "class" : "VI", "age" : 11, "grd_point" : 31.5201 }
>
>

> db.student.find({"age":{$lt:12}},{_id:0,f_name:1,sex:1,age:1,class:1});

{ "f_name" : "Tom", "sex" : "Male", "class" : "VI", "age" : 11 }
{ "f_name" : "Peter", "sex" : "Male", "class" : "VI", "age" : 11 }
>


> db.student.find({"age":{$lt:13}},

{_id:0,f_name:1,sex:1,age:1,class:1}).sort({grd_point:1});
{ "f_name" : "Tom", "sex" : "Male", "class" : "VI", "age" : 11 }
{ "f_name" : "Peter", "sex" : "Male", "class" : "VI", "age" : 11 }
{ "f_name" : "Zenny", "sex" : "Female", "class" : "VI", "age" : 12 }
>
>
>

> db.student.find({"age":{$lt:13}},{_id:0,f_name:1,sex:1,age:1,class:1,grd_point:1}).sort({grd_point:1});

{ "f_name" : "Tom", "sex" : "Male", "class" : "VI", "age" : 11, "grd_point" : 30.1257 }
{ "f_name" : "Peter", "sex" : "Male", "class" : "VI", "age" : 11, "grd_point" : 31.5201 }
{ "f_name" : "Zenny", "sex" : "Female", "class" : "VI", "age" : 12, "grd_point" : 32.6342 }
>
>
>
> db.student.find({"age":{$lt:12}},{_id:0,f_name:1,sex:1,age:1,class:1,grd_point:1}).sort({grd_point:1});

{ "f_name" : "Tom", "sex" : "Male", "class" : "VI", "age" : 11, "grd_point" : 30.1257 }
{ "f_name" : "Peter", "sex" : "Male", "class" : "VI", "age" : 11, "grd_point" : 31.5201 }
>
>

--@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ LIMITING AND SKIPPING THE DOCUMENTS @@@@@@@@@@@@@@@@@@@@@@@@@@@@@


--@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ VIEW LIMITED NUMBER OF DOCUMENTS @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@


--WITHOUT SKIPPING 
>
> db.student.find({"age":{$lt:13}},{_id:0,f_name:1,sex:1,age:1,class:1,grd_point:1}).sort({grd_point:1}).limit(2);
{ "f_name" : "Tom", "sex" : "Male", "class" : "VI", "age" : 11, "grd_point" : 30.1257 }
{ "f_name" : "Peter", "sex" : "Male", "class" : "VI", "age" : 11, "grd_point" : 31.5201 }
>
>
>
> db.doccollect.find({likes:{$lt:120}}, {by:1,title:1,_id:0,likes:1}).sort({by:1,likes:1}).limit(2);
{ "title" : "MongoDB doc10", "by" : "Ashok", "likes" : 80 }
{ "title" : "casandra doc1", "by" : "Ashok", "likes" : 100 }
>
>

> db.doccollect.find({likes:{$lt:120}}, {by:1,title:1,_id:0,likes:1}).sort({by:1,likes:1}).limit(4);
{ "title" : "MongoDB doc10", "by" : "Ashok", "likes" : 80 }
{ "title" : "casandra doc1", "by" : "Ashok", "likes" : 100 }
{ "title" : "MongoDB doc50", "by" : "Kamal", "likes" : 80 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
>

-- LIMIT 

> db.doccollect.find({likes:{$lt:120}}, {by:1,title:1,_id:0,likes:1}).sort({by:-1,likes:-1}).limit(5);


{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc50", "by" : "Kamal", "likes" : 80 }
{ "title" : "casandra doc1", "by" : "Ashok", "likes" : 100 }
>

-- LIMIT & SKIP condition 

> db.doccollect.find({likes:{$lt:120}}, {by:1,title:1,_id:0,likes:1}).sort({by:-1,likes:-1}).limit(5).skip(0);

{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc50", "by" : "Kamal", "likes" : 80 }
{ "title" : "casandra doc1", "by" : "Ashok", "likes" : 100 }
>


@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
TO SORT AND VIEW LIMITED NUMBER OF DOCUMENTS AND SKIPPING @@@@@@@@@@@@@@@@@@@@@@@@@@@@@

> db.doccollect.find({likes:{$lt:100}}, {by:1,title:1,_id:0,likes:1}).sort({likes:1}).limit(3).skip(0);

{ "title" : "MongoDB doc10", "by" : "Ashok", "likes" : 80 }
{ "title" : "MongoDB doc50", "by" : "Kamal", "likes" : 80 }
>
>
>
>
> db.doccollect.find({likes:{$lt:100}}, 
{by:1,title:1,_id:0,likes:1}).sort({likes:1}).limit(3).skip(1);---> from the output


{ "title" : "MongoDB doc50", "by" : "Kamal", "likes" : 80 }
>


> db.doccollect.find({likes:{$lt:120}}, {by:1,title:1,_id:0,likes:1}).sort({likes:1}).limit(3).skip(1);

{ "title" : "MongoDB doc50", "by" : "Kamal", "likes" : 80 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }


> db.doccollect.find({likes:{$lt:120}}, {by:1,title:1,_id:0,likes:1}).sort({likes:1}).limit(3).skip(2);

{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
{ "title" : "MongoDB doc100", "by" : "ashok", "likes" : 100 }
>

Monday, September 17, 2018

Manipulating Member State

Manipulating Member State


Turning Primaries into Secondaries


rs.stepDown(300);

MongoDB Enterprise OurReplica:PRIMARY> db.printReplicationInfo()
configured oplog size:   1436.840576171875MB
log length start to end: 51327secs (14.26hrs)
oplog first event time:  Fri Sep 14 2018 12:52:15 GMT+0530 (IST)
oplog last event time:   Sat Sep 15 2018 03:07:42 GMT+0530 (IST)
now:                     Sat Sep 15 2018 03:07:50 GMT+0530 (IST)
MongoDB Enterprise OurReplica:PRIMARY>



MongoDB Enterprise OurReplica:PRIMARY> rs.stepDown(300)
2018-09-15T02:36:43.720+0530 E QUERY    [js] Error: error doing query: failed: network                                                                                                        error while attempting to run command 'replSetStepDown' on host '127.0.0.1:6666'  :
DB.prototype.runCommand@src/mongo/shell/db.js:168:1
DB.prototype.adminCommand@src/mongo/shell/db.js:186:16
rs.stepDown@src/mongo/shell/utils.js:1398:12
@(shell):1:1
2018-09-15T02:36:43.722+0530 I NETWORK  [js] trying reconnect to 127.0.0.1:6666 failed
2018-09-15T02:36:43.722+0530 I NETWORK  [js] reconnect 127.0.0.1:6666 ok
MongoDB Enterprise OurReplica:SECONDARY>

Server 2 failover to Primary state now:

Preventing Election

If you need to do some maintenance on the Primary but don't want to any of the other eligible members to become Primary in the interim, you can force them to stay secondaries by running freeze on each of them.

port: 7777

MongoDB Enterprise OurReplica:SECONDARY> rs.freeze(1200)
{
        "ok" : 1,
        "operationTime" : Timestamp(1536959629, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1536959629, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}
MongoDB Enterprise OurReplica:SECONDARY>

port : 8888

MongoDB Enterprise OurReplica:SECONDARY> rs.freeze(1200)
{
        "ok" : 1,
        "operationTime" : Timestamp(1536959649, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1536959649, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}
MongoDB Enterprise OurReplica:SECONDARY>

port: 6666(Primary)

now: I tried to stepDown Primary server 6666. Let see what will happen now.

MongoDB Enterprise OurReplica:PRIMARY> rs.stepDown(300)
2018-09-15T02:44:58.439+0530 E QUERY    [js] Error: error doing query: failed: network error while attempting to run command 'replSetStepDown' on host '127.0.0.1:6666'  :
DB.prototype.runCommand@src/mongo/shell/db.js:168:1
DB.prototype.adminCommand@src/mongo/shell/db.js:186:16
rs.stepDown@src/mongo/shell/utils.js:1398:12
@(shell):1:1
2018-09-15T02:44:58.440+0530 I NETWORK  [js] trying reconnect to 127.0.0.1:6666 failed
2018-09-15T02:44:58.441+0530 I NETWORK  [js] reconnect 127.0.0.1:6666 ok
MongoDB Enterprise OurReplica:SECONDARY>
MongoDB Enterprise OurReplica:SECONDARY>
MongoDB Enterprise OurReplica:SECONDARY>
MongoDB Enterprise OurReplica:SECONDARY>

Verify the status of the replication, All nodes are secondaries and there is no primary in the replica set for 5 minutes ((we gave command rs.stepDown(300)). If you verify after 5 minutes in current secondary server (6666) it will be back to primary state automatically after our maintenance activity.


MongoDB Enterprise OurReplica:SECONDARY> rs.status();
{
        "set" : "OurReplica",
        "date" : ISODate("2018-09-14T21:16:32.594Z"),
        "myState" : 2,
        "term" : NumberLong(12),
        "syncingTo" : "",
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1536959689, 1),
                        "t" : NumberLong(12)
                },
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1536959689, 1),
                        "t" : NumberLong(12)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1536959689, 1),
                        "t" : NumberLong(12)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1536959689, 1),
                        "t" : NumberLong(12)
                }
        },
        "lastStableCheckpointTimestamp" : Timestamp(1536959689, 1),
        "members" : [
                {
                        "_id" : 0,
                        "name" : "127.0.0.1:6666",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 4100,
                        "optime" : {
                                "ts" : Timestamp(1536959689, 1),
                                "t" : NumberLong(12)
                        },
                        "optimeDate" : ISODate("2018-09-14T21:14:49Z"),
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "could not find member to sync from",
                        "configVersion" : 22,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 1,
                        "name" : "127.0.0.1:7777",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 4098,
                        "optime" : {
                                "ts" : Timestamp(1536959689, 1),
                                "t" : NumberLong(12)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1536959689, 1),
                                "t" : NumberLong(12)
                        },
                        "optimeDate" : ISODate("2018-09-14T21:14:49Z"),
                        "optimeDurableDate" : ISODate("2018-09-14T21:14:49Z"),
                        "lastHeartbeat" : ISODate("2018-09-14T21:16:32.485Z"),
                        "lastHeartbeatRecv" : ISODate("2018-09-14T21:16:32.469Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "configVersion" : 22
                },
                {
                        "_id" : 2,
                        "name" : "127.0.0.1:8888",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 3760,
                        "optime" : {
                                "ts" : Timestamp(1536959689, 1),
                                "t" : NumberLong(12)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1536959689, 1),
                                "t" : NumberLong(12)
                        },
                        "optimeDate" : ISODate("2018-09-14T21:14:49Z"),
                        "optimeDurableDate" : ISODate("2018-09-14T21:14:49Z"),
                        "lastHeartbeat" : ISODate("2018-09-14T21:16:32.485Z"),
                        "lastHeartbeatRecv" : ISODate("2018-09-14T21:16:32.486Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "configVersion" : 22
                },
                {
                        "_id" : 3,
                        "name" : "127.0.0.1:9999",
                        "health" : 1,
                        "state" : 7,
                        "stateStr" : "ARBITER",
                        "uptime" : 4098,
                        "lastHeartbeat" : ISODate("2018-09-14T21:16:32.485Z"),
                        "lastHeartbeatRecv" : ISODate("2018-09-14T21:16:32.485Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "configVersion" : 22
                }
        ],
        "ok" : 1,
        "operationTime" : Timestamp(1536959689, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1536959689, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}
MongoDB Enterprise OurReplica:SECONDARY>

I reconnected after 5 minutes in primary. It is back to earlier state. i.e. PRIMARY

MongoDB Enterprise OurReplica:SECONDARY> exit
bye
[root@nosqlDB mongodb]# mongo --port 6666
MongoDB shell version v4.0.0
connecting to: mongodb://127.0.0.1:6666/
MongoDB server version: 4.0.0
Server has startup warnings:
2018-09-15T01:38:12.762+0530 I STORAGE  [initandlisten]
2018-09-15T01:38:12.762+0530 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2018-09-15T01:38:12.762+0530 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2018-09-15T01:38:13.868+0530 I CONTROL  [initandlisten]
2018-09-15T01:38:13.868+0530 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-09-15T01:38:13.868+0530 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-09-15T01:38:13.868+0530 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-09-15T01:38:13.868+0530 I CONTROL  [initandlisten]
2018-09-15T01:38:13.869+0530 I CONTROL  [initandlisten]
2018-09-15T01:38:13.869+0530 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-09-15T01:38:13.869+0530 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-09-15T01:38:13.869+0530 I CONTROL  [initandlisten]
2018-09-15T01:38:13.869+0530 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-09-15T01:38:13.869+0530 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-09-15T01:38:13.869+0530 I CONTROL  [initandlisten]
MongoDB Enterprise OurReplica:PRIMARY


MongoDB Enterprise OurReplica:SECONDARY> rs.freeze(0);
{
        "info" : "unfreezing",
        "ok" : 1,
        "operationTime" : Timestamp(1536960082, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1536960082, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}

MongoDB Enterprise OurReplica:SECONDARY>


MongoDB Enterprise OurReplica:SECONDARY> rs.freeze(0)
{
        "info" : "unfreezing",
        "ok" : 1,
        "operationTime" : Timestamp(1536960092, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1536960092, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}

MongoDB Enterprise OurReplica:SECONDARY>





Step by Step YugabyteDB 2.11 (Open Source) Distributed DB - Multi-node Cluster Setup on RHEL

Scope - ·        Infrastructure planning and requirement for installation of the multi-node cluster database ·        Prerequisites Software...