MongoDB – Part 4: Operators

By | April 5, 2018

Lets work on the below data :

{ “_id” : ObjectId(“5ac5540cad718d31f9c60545”), “VodName” : “House Of Cards”, “Language” : “English”, “Genre” : [ “Politics”, “Thriller” ], “Category” : [ “Multi Part Video” ], “CountryPermission” : [ “India”, “USA”, “UK”, “UAE” ], “Rating” : 5 }

{ “_id” : ObjectId(“5ac5545cad718d31f9c60546”), “VodName” : “Elementary”, “Language” : “English”, “Genre” : [ “Crime”, “Thriller”, “Investigative” ], “Category” : [ “Multi Part Video” ], “CountryPermission” : [ “India”, “USA”, “UK” ], “Rating” : 3 }

{ “_id” : ObjectId(“5ac5549bad718d31f9c60547”), “VodName” : “Madam Secretary”, “Language” : “English”, “Genre” : [ “Politics” ], “Category” : [ “Multi Part Video” ], “CountryPermission” : [ “India”, “USA”, “UK” ], “Rating” : 5 }

{ “_id” : ObjectId(“5ac554dfad718d31f9c60548”), “VodName” : “Narcos”, “Language” : “English”, “Genre” : [ “Biography”, “Crime” ], “Category” : [ “Multi Part Video” ], “CountryPermission” : [ “USA”, “UK” ], “Rating” : 2 }

{ “_id” : ObjectId(“5ac55520ad718d31f9c60549”), “VodName” : “Blacklist”, “Language” : “English”, “Genre” : [ “Investigative”, “Crime” ], “Category” : [ “Multi Part Video” ], “CountryPermission” : [ “USA”, “UK” ], “Rating” : 3 }

$in Operator

> db.VOD.find({“Genre”:{$in:[“Politics”,”Thriller”]}})

{ “_id” : ObjectId(“5ac5540cad718d31f9c60545”), “VodName” : “House Of Cards”, “Language” : “English”, “Genre” : [ “Politics”, “Thriller” ], “Category” : [ “Multi Part Video” ], “CountryPermission” : [ “India”, “USA”, “UK”, “UAE” ], “Rating” : 5 }

{ “_id” : ObjectId(“5ac5545cad718d31f9c60546”), “VodName” : “Elementary”, “Language” : “English”, “Genre” : [ “Crime”, “Thriller“, “Investigative” ], “Category” : [ “Multi Part Video” ], “CountryPermission” : [ “India”, “USA”, “UK” ], “Rating” : 3 }

{ “_id” : ObjectId(“5ac5549bad718d31f9c60547”), “VodName” : “Madam Secretary”, “Language” : “English”, “Genre” : [ “Politics” ], “Category” : [ “Multi Part Video” ], “CountryPermission” : [ “India”, “USA”, “UK” ], “Rating” : 5 }

. Operator

 To show case this Let’s add additional row with metadata attribute added

> db.VOD.insert( {   “VodName”:”Breathe”,   “Language”: “English”,   “Genre”: [“Biography”,”Crime”],   “Category”:[“Multi Part Video”],   “CountryPermission”:[“India”,”USA”,”UK”],   “Rating”:2, “Metadata”:{“Actor”:”Madhavan”,”Year”:2009} } )

WriteResult({ “nInserted” : 1 })

. Operator works on the object only.

> db.VOD.find({“Metadata.Year”:2009})

{ “_id” : ObjectId(“5ac558f3ad718d31f9c6054a”), “VodName” : “Breathe”, “Language” : “English”, “Genre” : [ “Biography”, “Crime” ], “Category” : [ “Multi Part Video” ], “CountryPermission” : [ “India”, “USA”, “UK” ], “Rating” : 2, “Metadata” : { “Actor” : “Madhavan“, “Year” : 2009 } }

$and Operator

> db.VOD.find({$and:[{“Language”:”English”,”Rating”:2}]})

{ “_id” : ObjectId(“5ac554dfad718d31f9c60548”), “VodName” : “Narcos”, “Language” : “English”, “Genre” : [ “Biography”, “Crime” ], “Category” : [ “Multi Part Video” ], “CountryPermission” : [ “USA”, “UK” ], “Rating” : 2 }

{ “_id” : ObjectId(“5ac558f3ad718d31f9c6054a”), “VodName” : “Breathe”, “Language” : “English”, “Genre” : [ “Biography”, “Crime” ], “Category” : [ “Multi Part Video” ], “CountryPermission” : [ “India”, “USA”, “UK” ], “Rating” : 2, “Metadata” : { “Actor” : “Madhavan”, “Year” : 2009 } }

> db.VOD.find({$and:[{“Language”:”French”,”Rating”:2}]})

>

$or Operator

> db.VOD.find({$or:[{“Language”:”French”},{“Rating”:2}]})

{ “_id” : ObjectId(“5ac554dfad718d31f9c60548”), “VodName” : “Narcos”, “Language” : “English”, “Genre” : [ “Biography”, “Crime” ], “Category” : [ “Multi Part Video” ], “CountryPermission” : [ “USA”, “UK” ], “Rating” : 2 }

{ “_id” : ObjectId(“5ac558f3ad718d31f9c6054a”), “VodName” : “Breathe”, “Language” : “English”, “Genre” : [ “Biography”, “Crime” ], “Category” : [ “Multi Part Video” ], “CountryPermission” : [ “India”, “USA”, “UK” ], “Rating” : 2, “Metadata” : { “Actor” : “Madhavan”, “Year” : 2009 } }

$gte,$gt,$lte,$lt Operators

>> db.VOD.find({“Rating”:{$gte:3}})

{ “_id” : ObjectId(“5ac5540cad718d31f9c60545”), “VodName” : “House Of Cards”, “Language” : “English”, “Genre” : [ “Politics”, “Thriller” ], “Category” : [ “Multi Part Video” ], “CountryPermission” : [ “India”, “USA”, “UK”, “UAE” ], “Rating” : 5 }

{ “_id” : ObjectId(“5ac5545cad718d31f9c60546”), “VodName” : “Elementary”, “Language” : “English”, “Genre” : [ “Crime”, “Thriller”, “Investigative” ], “Category” : [ “Multi Part Video” ], “CountryPermission” : [ “India”, “USA”, “UK” ], “Rating” : 3 }

{ “_id” : ObjectId(“5ac5549bad718d31f9c60547”), “VodName” : “Madam Secretary”, “Language” : “English”, “Genre” : [ “Politics” ], “Category” : [ “Multi Part Video” ], “CountryPermission” : [ “India”, “USA”, “UK” ], “Rating” : 5 }

{ “_id” : ObjectId(“5ac55520ad718d31f9c60549”), “VodName” : “Blacklist”, “Language” : “English”, “Genre” : [ “Investigative”, “Crime” ], “Category” : [ “Multi Part Video” ], “CountryPermission” : [ “USA”, “UK” ], “Rating” : 3 }

> db.VOD.find({“Rating”:{$gt:3}})

{ “_id” : ObjectId(“5ac5540cad718d31f9c60545”), “VodName” : “House Of Cards”, “Language” : “English”, “Genre” : [ “Politics”, “Thriller” ], “Category” : [ “Multi Part Video” ], “CountryPermission” : [ “India”, “USA”, “UK”, “UAE” ], “Rating” : 5 }

{ “_id” : ObjectId(“5ac5549bad718d31f9c60547”), “VodName” : “Madam Secretary”, “Language” : “English”, “Genre” : [ “Politics” ], “Category” : [ “Multi Part Video” ], “CountryPermission” : [ “India”, “USA”, “UK” ], “Rating” : 5 }

> db.VOD.find({“Rating”:{$lt:3}})

{ “_id” : ObjectId(“5ac554dfad718d31f9c60548”), “VodName” : “Narcos”, “Language” : “English”, “Genre” : [ “Biography”, “Crime” ], “Category” : [ “Multi Part Video” ], “CountryPermission” : [ “USA”, “UK” ], “Rating” : 2 }

{ “_id” : ObjectId(“5ac558f3ad718d31f9c6054a”), “VodName” : “Breathe”, “Language” : “English”, “Genre” : [ “Biography”, “Crime” ], “Category” : [ “Multi Part Video” ], “CountryPermission” : [ “India”, “USA”, “UK” ], “Rating” : 2, “Metadata” : { “Actor” : “Madhavan”, “Year” : 2009 } }

> db.VOD.find({“Rating”:{$lte:3}})

{ “_id” : ObjectId(“5ac5545cad718d31f9c60546”), “VodName” : “Elementary”, “Language” : “English”, “Genre” : [ “Crime”, “Thriller”, “Investigative” ], “Category” : [ “Multi Part Video” ], “CountryPermission” : [ “India”, “USA”, “UK” ], “Rating” : 3 }

{ “_id” : ObjectId(“5ac554dfad718d31f9c60548”), “VodName” : “Narcos”, “Language” : “English”, “Genre” : [ “Biography”, “Crime” ], “Category” : [ “Multi Part Video” ], “CountryPermission” : [ “USA”, “UK” ], “Rating” : 2 }

{ “_id” : ObjectId(“5ac55520ad718d31f9c60549”), “VodName” : “Blacklist”, “Language” : “English”, “Genre” : [ “Investigative”, “Crime” ], “Category” : [ “Multi Part Video” ], “CountryPermission” : [ “USA”, “UK” ], “Rating” : 3 }

{ “_id” : ObjectId(“5ac558f3ad718d31f9c6054a”), “VodName” : “Breathe”, “Language” : “English”, “Genre” : [ “Biography”, “Crime” ], “Category” : [ “Multi Part Video” ], “CountryPermission” : [ “India”, “USA”, “UK” ], “Rating” : 2, “Metadata” : { “Actor” : “Madhavan”, “Year” : 2009 } }

Suppress certain columns :

> db.VOD.find({},{VodName:1,Language:1,_id:0} )

{ “VodName” : “House Of Cards”, “Language” : “English” }

{ “VodName” : “Elementary”, “Language” : “English” }

{ “VodName” : “Madam Secretary”, “Language” : “English” }

{ “VodName” : “Narcos”, “Language” : “English” }

{ “VodName” : “Blacklist”, “Language” : “English” }

{ “VodName” : “Breathe”, “Language” : “English” }

Tip :

> db.VOD.find({},{VodName:1,Language:0,_id:0} )

Error: error: {

        “ok” : 0,

        “errmsg” : “Projection cannot have a mix of inclusion and exclusion.”,

        “code” : 2,

        “codeName” : “BadValue”

}

Note that you can always include or exclude not both. In the above example we tried to include VodName and exclude Language. Only _id column could be either way. 

Leave a Reply

Your email address will not be published. Required fields are marked *