Introduction to MongoDB

for Javascript Developers

About Me

SRI SAI SWAROOP KOMMINENI

Full Stack Javascript Developer

B. Tech and M. Tech from IIT Bombay

Course Structure

CHAPTER 1: INTRODUCTION

CHAPTER 2: CRUD

CHAPTER 3: SCHEMA DESIGN

CHAPTER 4: PERFORMANCE

CHAPTER 5: AGGREGATION FRAMEWORK

CHAPTER 6: APPLICATION ENGINEERING

CHAPTER 7: MONGOOSE                                     

 

50 % Lab

50 % Theory

CHAPTER 1: INTRODUCTION

JSON

  • JSON - JavaScript Object Notation
  • lightweight data-interchange format
  • easy for humans to read and write
  • easy for machines to parse and generate.
  • based on a subset of the JavaScript Language
  • text format that is completely language independent
  1. A collection of name/value pairs.
  2. An ordered list of values.

JSON is built on two structures:

{
  "key": "value"
}

["element1", "element2", "element3"]
{}

{
  "key": "value"
}

{
  "string": "Hello World",
  "boolean": true,
  "boolean2": false,
  "null": null,
  "number": 123,
  "number1": 123.25,
  "number1": 12e2,
  "array": [ 1, "2", 3.5, false, null ],
  "object": {
    "a": "b",
    "c": null,
    "e": false
  }
}
[]

[ 1, 2, 3]

["string", false, null, { "name" : "sai" }]


[
    "string",
    false,
    null,
    { 
        "name" : "your name"
    },
    { 
        "month" : "August",
        "day" : 15,
        "birthday": false,
        "SQL" : null
    }
]

Objects

Arrays

Which of the following data types are directly supported by JSON per the JSON spec?

  • String
  • Array
  • Object
  • Boolean values (true and false)
  • Floating-point number
  • Null
  • Pointer
  • Date
  • Graph
  • Decimal number

What is MongoDB?

  • Non relational datastore (not in tables and rows)
  • Stores JSON-like documents
  • Schema less - two documents need not have same keys

 

MongoDB is a document database with the scalability and flexibility that you want with the querying and indexing that you need

 

MongoDB eliminates a number of technical challenges that slow the pace of app development. 

  • MongoDB stores data in flexible, JSON-like documents, meaning fields can vary from document to document and data structure can be changed over time

  • The document model maps to the objects in your application code, making data easy to work with

  • Ad hoc queries, indexing, and real time aggregation provide powerful ways to access and analyze your data

  • MongoDB is a distributed database at its core, so high availability, horizontal scaling, and geographic distribution are built in and easy to use

  • MongoDB is free and open-source, published under the GNU Affero General Public License

Compass

Compass is a visual interface to MongoDB thats lets us connect to the database and explore the datasets. 

 BSON

MongoDB represents JSON documents in binary-encoded format called BSON behind the scenes. BSON extends the JSON model to provide additional data types, ordered fields, and to be efficient for encoding and decoding within different languages.

Query Language

MongoDB queries are represented as a JSON-like structure, just like documents. To build a query, you specify a document with properties you wish the results to match.

MongoDB Relative to Relational

Atomicity is an all-or-none proposition.

 

Consistency guarantees that a transaction never leaves your database in a half-finished state.

 

Isolation keeps transactions separated from each other until they’re finished.

 

Durability guarantees that the database will keep track of pending changes in such a way that the server can recover from an abnormal termination.

ACID

Query Language

Installation

Compass

  • connecting to local mongodb
  • databases, collections, documents
  • connecting to atlas
  • exploring datasets
  • scalar value types and analyze schema in compass: movies.video
  • Fields with Documents as Values : 100Y weather
  • Fields with Arrays as Values
  • Nested documents - skyCoverLayer
  • Geospatial Data - position : calculate distances, filter for radius etc.,
  • Filtering Collections with Queries - citibike.trips, end station name, birth year, trip duration
  • Geospatial Queries - ship.shipwrecks

Which of the following statements are true? Check all that apply.

  1. Documents are stored in collections.

  2. A database may contain one or more collections.

  3. Each database and collection combination define a namespace.

  4. We reference a namespace using the name of the database, followed by a comma, followed by the name of the collection, e.g., city,neighborhoods.

  5. We won't talk about indexes too much in this chapter

     

Which of the following are types of data Compass (and MongoDB) recognizes and specifically supports?

  • documents
  • arrays
  • geospatial data
  • rainfall
  • air pressure

 

Which of statements below best describes the following filter?

{"age": {"$gte": 21, "$lt": 70}}
  • Find all documents for which the age field has a value that is >= 21 and <= 70.
  • Find all documents for which the age field has a value that is either equal to 21 or equal to 70.
  • Find all documents for which the age field has a value that is >= 21 and < 70.
  • Find all documents for which the age field is < 70.
  • None of the above.

Deep Dive into operators

 

  • Comparision: $lt, $gte
  • meta: $type, $exists:true, false 
  • Logical Operators: $or , $and
  • Array Operators: $all, $size, $elemMatch
  • Evaluation: $regex

 

CHAPTER 2: CRUD

Quick intro to the mongo shell 

  • mongo - command to connect to the mongodb shell
  • show dbs - lists the databases in current mongodb
  • use demo - switches to this db, creates if it does not exist
  • db.things.find() - finds the documents in things collection
  • db.things.find().pretty()
  • cls - clear screen
  • db.things.insert(), db.things.save() - Create new docouments
  • what happens when u dont pass a proper JSON
  • Javascript environment - for loop example
  • db.things.find().limit(1) - limits the no. of results to 1
  • db.things.find({"a":1}) - find documents where a is 1
  • Execute javascript with mongo shell !!
  • connect to atlas cluster from shell.
  • To make it possible for all the nodes to communicate with each other
  • There really isn't a good reason
  • So that other nodes in the cluster can contact our client, if necessary
  • So that if the primary node goes down, the shell can connect to other nodes in the cluster instead
  • Because our authentication credentials are not stored on the primary, but on other nodes in our cluster.

When connecting to an Atlas cluster using the shell, why do we provide the hostnames for all nodes when we launch mongo? Choose the best answer from the choices below.

Loding datasets

  • Setting the working directory
  • load("filename.js") 
  • show dbs, use videos 
  • use compass to view the data

Creating Documents

  • using compass to insert couple of movies
  • db.coll.insertOne({})
  • all documents in mongdodb must contain _id as its unique id for each document.

insertOne()

insertMany()

  • for inserting many documents - pass an array
  • second argument is options
  • {ordered: false}, default ordered insert stops at first error

Other way to insert is using an update operation which is called Upsert

Which of the following statements are true of creating documents in MongoDB?

  • All _id values within a single collection must be unique.
  • MongoDB or the client will create an _id for us if we do not supply one.
  • Both the mongo shell and Compass support the ability to insert single documents.
  • We can supply an _id for the document.
  • _id values created for us by MongoDB are of type int32.

Reading Documents

  • somewhat similar to filters which we used earlier
  • db.coll.find({})
  • dot notation to filter nested documents
  • its a good practise to use quotes when using dot notation. 
  • cast: ["", ""]
  • cast.0
  • find method returns a cursor which is a pointer
  • 'it' (iterate) keyword can be used to get next 20 results.

find()

Projections

  • second parameter for the find method
  • {"title": 1, _id:0} - returns only title instead of whole object
  • reduces network overhead
  • 1s to include feilds, 0 to exclude feilds

Updating Documents

 

  • first parameter is the same filter operation - {"title" :"XYZ"}
  • second parameter says how we want to update, need to use one of the update operators like $set - {$set: {"poster": "xyz"} }
  • if there are many, first one will be updated. 
  • can be used to update nested objects as well. 

updateOne()

Update Operators

 

  • $push - add array and push into it for example reviews array
  • To specify how one or more fields should be modified in matching documents.
  • As filters to identify documents that should be updated.
  • To identify fields to be updated in matching documents.
  • To replace matching documents in update operations.

Which of the following best describes the purpose of update operators? choose best answer.

Updating Documents

 

  • same as updateOne functionality except one
  • all those which matched the filters will get updated
  • Usually used for data cleaning purposes
  • rated:null,  $unset : {rated: ""}  - removes rated field if its null. 

updateMany()

uspert

third parameter for the updateOne function


    var detail = {} // the actual movie object
    db.movieDetails.updateOne({
         "imdb.id”: detail.imdb.id
    }, {
        $set: detail
    }, {
        upsert: true
    })

Updating Documents

 

replaceOne()

  • for cases where you want to completely replace the document no matter what you already have. 

    var detailDoc = {} // the actual movie object
    db.movieDetails.replaceOne({
         "imdb.id”: detailDoc.imdb.id
    }, 
        detailDoc
    )

Suppose you wish to update the value of the plot field for one document in our movieDetailscollection to correct a typo. Which of the following update operators and modifiers would you need to use to do this?

  • $slice
  • $set
  • $push
  • $mul
  • $addToSet
  • $inc
  • $rename
  • $unset
  • $position

Deleting Documents

 

  • deletes first match on our filter

deleteOne()

deleteMany()

  • deletes all matches on our filter
Made with Slides.com