MongoDB Atlas & Microsoft Azure

Hands-On Exercises

Solutions Architect @ MongoDB | chris.grabosky@mongodb.com
Deck & Content | l.gsky.us/#!mdbmsft2018
Registration Link | l.gsky.us/#!mdbmsft2018reg
Click Here For Printable Version

Terminology Check

Exercise 1

MongoDB Atlas & Users

At the end of this Exercise 1 you should be able to...

  • Deploy a MongoDB Atlas Cluster (free-tier M0 or paid M10+)
  • Understand basics of creating users and controls in MongoDB Atlas
  • Know the difference between Atlas users and MongoDB Users

Deploy M0 on Azure

Users

db.createUser()

Exercise 2

MongoDB Compass & CRUD

At the end of this Exercise 2 you should be able to...

  • Launch Compass (tool to explore, visualize, optomize, and modify MongoDB data) and connect to that cluster
  • Use Compass to create a database, create a collection, and import a data set into that collection
  • Use Compass to create, read, update, and delete (CRUD) documents in a collection

Connecting to Atlas with Compass

Connecting to Atlas with Compass

Creating a DB & Collection

Importing Data

Dataset: l.gsky.us#!moviedataset

Connection Strings

If your cluster works, use it, but...

KeyValue
Servermdbazuretraining-v9iwf.azure.mongodb.net
SRV RecordYes on Switch
AuthenticationUsername/Password
Usernameazureacct
Passwordazurepasswd
Auth DBadmin

M001 CRUD: Let's Create...

db.movies.insertOne({})

Schema & Data Types

M001 CRUD: Let's Read...

db.movies.find()

M001 CRUD: Let's Update...

db.movies.updateOne({},{$set:{}})

M001 CRUD: Let's Delete...

db.movies.deleteOne()

M001 CRUD: Find me movies...

QuestionAnswer
From 1987
{year:1987}
"Comedy" as one of their genres
{genre:"Comedy"}
"Comedy" as only genre
{genres:["Comedy"]}
"Comedy" or "Drama"
{genres:{$in:["Comedy", "Drama"]}}
"Comedy" and "Drama
{ genres: { $all: ["Comedy", "Drama"] } }
IMDB Rating > 8.0 and PG Rating
{"imdb.rating" : {$gt: 8.0}, rated:"PG"}
Whole title of "Dr. Strangelove"
{title: {$regex: '^Dr. Strangelove'}}

Exercise 3

Indexes & Explain Plan

At the end of this Exercise 3 you should be able to...

  • Identify how a query was run using the Explain Plan
  • Create an index on a collection
  • See how an index affects query plans

Explain Plan

Explain Plan

.find({"address.zipcode": {$gt: '5000'}, cuisine: 'Sushi'}).sort({stars:-1})

  • Not a good result...
  • ColScan and In-Memory Sort
  • We need indexes

What Index To Use?

.find({"address.zipcode": {$gt: '5000'}, cuisine: 'Sushi'}).sort({stars:-1})

  • Naive approach would be
    db.createIndex({"address.zipcode":1, "cuisine":1, "stars":1})
  • While better since it does an index scan, still doing an in-memory sort since this is a range query, not equality
  • Add the index above and compare
    .find({"address.zipcode": {$gt: '5000'}, cuisine: 'Sushi'}).sort({stars:-1})
    versus
    .find({"address.zipcode": '5000', cuisine: 'Sushi'}).sort({stars:-1})

Adding Index

ESR Rule

  • Great rule of thumb
  • Remove your last index
  • Remember Equality, Sort, Range
  • db.createIndex({"cuisine":1, "stars":1, "address.zipcode":1})

Exercise 4

Aggregation Framework

At the end of this Exercise 4 you should be able to...

  • Have a basic understanding of what the aggregation framework is
  • Write some basic queries using the aggregation framework

Aggregation Framework

University M121

Aggregation Framework

db.col.aggregate([{stage1},{stage2},{stagen}])

Agg Workshop

QuestionAnswer
That Are Comedies
$match {genres:"Comedy"}
Have a rating
$match {rated:{$exists:true}}
Just the title and how many years old
$project {
    _id:0,
    title:1,
    yearsOld: { $subtract: [2018,"$year"]}
}
                                    

1 Degree of Kevin Bacon

Stage 1
$project {
    title:1, actors:1,_id:0
}
                                    
Stage 2
$graphLookup {
    from: 'movies',
    startWith: "$actors",
    connectFromField: 'actors',
    connectToField: 'actors',
    as: 'foo',
    maxDepth: 1,
    depthField: 'degrees',
    restrictSearchWithMatch: {actors:"Kevin Bacon"}
}
                                    
Stage 3
$project {
    title:1,actors:1,_id:0,"foo.title":1,"foo.actors":1,jumps:{$size:"$foo"}
}
                                    
Stage 4
$match {
    jumps:{$gt:0}
  }
                                        

Exercise 5

BI Connector

At the end of this Exercise 5 you should be able to...

  • Enable BI Connector on a cluster
  • Connect to a MongoDB cluster using a SQL CLI or BI tool like PowerBI

Enable BI Connector

https://docs.mongodb.com/bi-connector/current/connect/powerbi/

Now We Talk SQL

Eww

Configuring ODBC

https://docs.mongodb.com/bi-connector/current/connect/powerbi/

Configuring ODBC

https://docs.mongodb.com/bi-connector/current/connect/powerbi/

Let's Make Some Dashboards

Use your cluster with Movies DB

Or my cluster has lots of choices...

Exercise 6

Stitch & Integrations

At the end of this Exercise 6 you should be able to...

  • Understand MongoDB Stitch serverless platform capabilities
  • Create Stitch triggers on Change Streams, functions, etc

Stitch Components

Serverless Scenarios

AppAtlas TechAzure TechLink
Bowling TrackerQuery Anywhere
WebHooks
Trigger Functions
Web Appl.gsky.us/#!bowlsrc
"Instagram"Query Anywhere
WebHooks
Trigger Functions
Web App
Azure Storage
Xamarin App
Cognitive Vision
l.gsky.us/#!igsrc
"Tiny URL"WebHooksWeb Appl.gsky.us/#!linksrc

"Instagram" App

Uploading

"Instagram" App

Browsing

Exercise 7

Big Data

At the end of this Exercise 7 you should be able to...

http://l.gsky.us#!msftspark

  • Register the MongoDB Spark Connector on Azure Databricks
  • Write data to MongoDB
  • Load data from a MongoDB collection
  • Load data from an aggregation framework pipeline
  • Run Spark SQL

Download HTML Code

http://l.gsky.us#!msftspark

Thank You

cloud.mongodb.com
azure.microsoft.com