MongoDB is a nosql database. This means that you don’t use SQL to query the data, instead you use javascript and receive responses in json objects. I enrolled in course M001: Mongodb basics to get a basic understanding of how it works and it seems extremely easy.
The cool part about it being javascript based is that you can become a “full stack” web developer just by learning javascript. The front end with angular, and the backend with node.js and mongodb can be sufficient for you to build web apps. (I still shudder thinking that javascript is being used for anything, yet here we are..)
Mongodb is extremely easy to start playing with, for free you can create a database cluster in the cloud with 512MB by registering in mongodb.com and getting a Mongodb atlas.
MongoDB Compass is a graphical client with which you can connect to your database and analyse your data. It analyses the data and gives you insights on the types of data, you can also filter just by selecting with your mouse the data you want to filter on in an intuitive way.
The CLI is the best way to power-query your data. below my cheat-sheet after completing the basic training course:
MongoDB cheatsheet
- Show databases
- Shows the databases
- Show collections
- Once within a database , shows the collections
- Insert data
- db.moviesScratch.insertMany([],{“ordered”:false)
- With ordered:false, it does not stop when a duplicate _id key is found,
- db.moviesScratch.insertMany([],{“ordered”:false)
- Query data
- db.movies.find().pretty()
- Comparison operators
- Greater than
- db.movieDetails.find({runtime:{$gt:90}})
- Greater than and Less than
- db.movieDetails.find({runtime:{$gt:90, $lt:120}},{_id:0,title:1,runtime:1})
- Greater or equal, less or equal
- db.movieDetails.find({runtime:{$gte:90, $lte:120}},{_id:0,title:1,runtime:1})
- Not equals
- db.movieDetails.find({rated: {$ne:”UNRATED”}},{_id:0,title:1,runtime:1})
- In
- db.movieDetails.find({rated: {$in: [“G”,”PG”]}},{_id:0,title:1,runtime:1,rated:1})
- Greater than
- Filter for null or non existing fields
- db.movies.find({mpaaRating:{$exists:true}}).pretty()
- db.data.find({atmosphericPressureChange:{$exists:false}}).pretty().count()
- Filter fields by type (double, int, string…)
- db.movies.find({viewerRating:{$type:”double”}})
- Filter with multiple “OR”
- db.movieDetails.find({$or: [{“tomato.meter”:{$gt:95}},{“metacritic”:{$gt:88}}]},{_id:0,title:1,”tomato.meter”:1,”metacritic”:1})
- db.shipwrecks.find({$or:[{watlev:”always dry”},{depth:0}]}).count()
- Filter with multiple “AND” (this is only needed when we want to apply many filters on the same key
- db.movieDetails.find({$and: [{“metacritic”:{$ne:null}},{“metacritic”:{$exists:true}}]},{_id:0,title:1,”tomato.meter”:1,”metacritic”:1})
- Array field moderators
- Get all objects which contain elements in array
- db.movieDetails.find({genres:{$all: [“Comedy”,”Crime”,”Drama”]}},{_id:0, title:1, genres:1}).pretty()
- Get size of an array
- db.movieDetails.find({countries:{$size:1}}).count()
- db.data.find({sections:{$size:2}}).count()
- Find a single object with multiple elements which match all the criteria
- db.movieDetails.find({boxOffice: {$elemMatch: {“country”: “Germany”, “revenue”: {$gt: 16}}}})
- db.surveys.find({results:{$elemMatch:{“product”:”abc”,”score”:7}}}).count()
- Get all objects which contain elements in array
- Regular expressions
- db.movieDetails.find({“awards.text”: {$regex: /^Won.* /}}, {_id: 0, title: 1, “awards.text”: 1}).pretty()