본문 바로가기

프로그래밍/MongoDB

M001 MongoDB Basics

728x90
반응형

아래 링크에서 무료이므로 관심 있으시면 들으시면 됩니다.

https://university.mongodb.com/courses/M001/about

 

MongoDB Basics | M001 | MongoDB University

MongoDB Basics is designed for learners brand new to MongoDB. In this course you will learn how to set up your database and start exploring different ways to search and create data with MongoDB. We will also cover how to use Compass, the MongoDB GUI, and d

university.mongodb.com

What You'll Learn

  • How to install MongoDB
  • How to use Compass, the MongoDB GUI
  • The MongoDB document model and basic schema design
  • The MongoDB query language
  • How to use Atlas, MongoDB's hosted database as a service offering

Agenda

  • Chapter 1: Introduction
  • Chapter 2: The MongoDB Query Language + Atlas
  • Chapter 3: Deeper Dive on the MongoDB Query Language
  • Final Exam

Chapter 1: Introduction

Lecture: Welcome!

Lecture: Grading and Logistics

  • Quizzes - ungraded
  • Labs/Homework - 50%
  • Final Exam - 50%
  • Once we've passed the due date for the final exam, if you've achieved a grade of 65% or better, you'll receive a statement of completion for this course.

Lecture: Are you Behind a Firewall?

  • In order to continue in this course, you must be able to make outgoing requests from your computer to database servers we have set up in MongoDB Atlas. Those servers run on port 27017 in Amazon AWS.
  • Please confirm that port 27017 is not blocked by clicking http://portquiz.net:27017

Lecture: Connecting to MongoDB Using Compass

In this course we will make extensive use of MongoDB Compass for learning how to use MongoDB.

1. Please download Compass from the MongoDB Download Center . If you downloaded Compass before today, please make sure you are using latest (Stable) version of Compass and upgrade if necessary. Please ensure you don't download "Community Edition Stable" version.

  1. Install Compass on your computer from the download.
  2. Launch Compass.

When Compass opens you will see a page titled "Connect to Host".

 

  1. Use the following information to complete this form, but do not click "Connect" yet.

    Hostname: cluster0-shard-00-00-jxeqq.mongodb.net

    Username: m001-student

    Password: m001-mongodb-basics

    Replica Set Name: Cluster0-shard-0

    Read Preference: Primary Preferred

  2. Click "Add to Favorites" and enter M001 RS as the Favorite Name. Adding this connection as a favorite will enable you to easily connect to our class MongoDB deployment after closing and restarting Compass at some point in the future.

  3. Now, click "Connect" and load the databases in the M001 class MongoDB deployment.

회사에서는 SSL : Unvalidated (insecure) 로 설정

Lecture: Databases, Collections, and Documents

 

Lecture: Exploring Datasets in Compass

  • Lab 1.1: Install Compass and Connect

Lecture: Documents: Scalar Value Types

Lecture: MongoDB Documents: Fields with Documents as Values

  • The value of air temperature is actually a nested document with two fields, quality and value.

Lecture: MongoDB Documents: Fields with Arrays as Values

 

  • Lab 1.2: Determine the Value Type, Part 1
  • Lab 1.3: Determine the Value Type, Part 2
  • Lab 1.4: Determine the Value Type, Part 3

Lecture: MongoDB Documents: Geospatial Data

Lecture: Filtering Collections with Queries

  • Lab 1.5: Scavenger Hunt, Part 1
  • Lab 1.6: Scavenger Hunt, Part 2

Lecture: Geospatial Queries

  • Lab 1.7: Scavenger Hunt, Part 3

Lecture: Understanding JSON

Chapter 2: The MongoDB Query Language + Atlas

Lecture: Introduction to CRUD

 

Lecture: Installing the mongo Shell (Windows)
Lecture: Installing the mongo Shell (OSX / Linux)

Lecture: Connecting to Our Class Atlas Cluster from the mongo Shell

atlas student

mongo "mongodb://cluster0-shard-00-00-jxeqq.mongodb.net:27017,cluster0-shard-00-01-jxeqq.mongodb.net:27017,cluster0-shard-00-02-jxeqq.mongodb.net:27017/test?replicaSet=Cluster0-shard-0" --authenticationDatabase admin --ssl --username m001-student --password m001-mongodb-basics

Lecture: Creating an Atlas Sandbox Cluster

  • https://www.mongodb.com/cloud
  • Lecture: Lab 2.0: Create an Atlas Sandbox Cluster (Ungraded)
    • Netwokr Access > IP Whitelist에 추가할 회사 IP : 61.36.36.116

Lecture: Connecting to Your Sandbox Cluster from the mongo Shell

altas my

mongo "mongodb+srv://cluster0-ubbe3.mongodb.net/test" --authenticationDatabase admin --ssl --username m001-student --password m001-mongodb-basics

local

mongo --host 127.0.0.1 --port 27017 


Lecture: Loading Data into Your Sandbox Cluster

  • loadMovieDetailsDataset.zip

load("loadMovieDetailsDataset.js")

Lecture: Connecting to Your Sandbox Cluster from Compass

  • Lab 2.1: How Many Comedies?

Lecture: Creating Documents: insertOne()
Lecture: Creating Documents: insertMany()

insertMany-ordered.js

db.moviesScratch.insertMany(

 [

 {

 "_id" : "tt0084726",

 "title" : "Star Trek II: The Wrath of Khan",

 "year" : 1982,

 "type" : "movie"

 },

 {

 "_id" : "tt0796366",

 "title" : "Star Trek",

 "year" : 2009,

 "type" : "movie"

 },

 {

 "_id" : "tt0084726",

 "title" : "Star Trek II: The Wrath of Khan",

 "year" : 1982,

 "type" : "movie"

 },

 {

 "_id" : "tt1408101",

 "title" : "Star Trek Into Darkness",

 "year" : 2013,

 "type" : "movie"

 },

 {

 "_id" : "tt0117731",

 "title" : "Star Trek: First Contact",

 "year" : 1996,

 "type" : "movie"

 }

 ]

);

insertMany-unordered.js

db.moviesScratch.insertMany(

 [

 {

 "_id" : "tt0084726",

 "title" : "Star Trek II: The Wrath of Khan",

 "year" : 1982,

 "type" : "movie"

 },

 {

 "_id" : "tt0796366",

 "title" : "Star Trek",

 "year" : 2009,

 "type" : "movie"

 },

 {

 "_id" : "tt0084726",

 "title" : "Star Trek II: The Wrath of Khan",

 "year" : 1982,

 "type" : "movie"

 },

 {

 "_id" : "tt1408101",

 "title" : "Star Trek Into Darkness",

 "year" : 2013,

 "type" : "movie"

 },

 {

 "_id" : "tt0117731",

 "title" : "Star Trek: First Contact",

 "year" : 1996,

 "type" : "movie"

 }

 ],

 {

 "ordered": false

 }

);

  • "ordered": false 를 하면 duplicate key error가 발생해도 stop 되지 않고 나머지 document 들은 insert 됨.
  • Lab 2.2: How Many Inserted?

Lecture: Reading Documents: Scalar Fields

  • Lab 2.3: Queries on Scalar Fields

Lecture: Reading Documents: Array Fields

{ "cast.0": "Jeff Bridges" }

  • Lab 2.4: Queries on Array Fields, Part 1
  • Lab 2.5: Queries on Array Fields, Part 2

Lecture: Cursors

  • In the shell, the cursor is automatically iterated up to 20 times to print an initial set of search results, as we see here in response to this query.

Lecture: Projections

Lecture: Updating Documents: updateOne()

Lecture: Update Operators

Lecture: Updating Documents: updateMany()

Lecture: Upserts

 

Lecture: Updating Documents: replaceOne()

replaceOne.js

let filter = {title: "House, M.D., Season Four: New Beginnings"}

let doc = db.movieDetails.findOne(filter);

doc.poster;

doc.poster = "https://www.imdb.com/title/tt1329164/mediaviewer/rm2619416576";

doc.genres;

doc.genres.push("TV Series");

db.movieDetails.replaceOne(filter, doc);

  • Lab 2.6: Update Operators

Lecture: Deleting Documents

Chapter 3: Deeper Dive on the MongoDB Query Language

Lecture: Introduction to Query Operators
Lecture: Comparison Operators

m001_comparisonOperators.js

db.movieDetails.find({runtime: {$gt: 90}})

db.movieDetails.find({runtime: {$gt: 90}}, {_id: 0, title: 1, runtime: 1})

db.movieDetails.find({runtime: {$gt: 90, $lt: 120}}, {_id: 0, title: 1, runtime: 1})

db.movieDetails.find({runtime: {$gte: 90, $lte: 120}}, {_id: 0, title: 1, runtime: 1})

db.movieDetails.find({runtime: {$gte: 180}, "tomato.meter": 100}, {_id: 0, title: 1, runtime: 1})

db.movieDetails.find({rated: {$ne: "UNRATED"}}, {_id: 0, title: 1, rated: 1})

db.movieDetails.find({rated: {$in: ["G", "PG"]}}, {_id: 0, title: 1, rated: 1})

db.movieDetails.find({rated: {$in: ["G", "PG", "PG-13"]}}, {_id: 0, title: 1, rated: 1}).pretty()

db.movieDetails.find({rated: {$in: ["R", "PG-13"]}}, {_id: 0, title: 1, rated: 1}).pretty()

Lecture: Element Operators

m001_element_operators.js

db.moviesDetails.find({mpaaRating: {$exists: true}})

db.moviesDetails.find({mpaaRating: {$exists: false}})

db.movieDetails.find({mpaaRating: null})

db.movieDetails.find({})

db.movies.find({viewerRating: {$type: "int"}}).pretty()

db.movies.find({viewerRating: {$type: "double"}}).pretty()

Lecture: Logical Operators

m001_logical_operators.js

db.movieDetails.find({$or: [{"tomato.meter": {$gt: 95}},

 {"metacritic": {$gt: 88}}]},

 {_id: 0, title: 1, "tomato.meter": 1, "metacritic": 1})

db.movieDetails.find({$and: [{"tomato.meter": {$gt: 95}},

 {"metacritic": {$gt: 88}}]},

 {_id: 0, title: 1, "tomato.meter": 1, "metacritic": 1})

db.movieDetails.find({"tomato.meter": {$gt: 95},

 "metacritic": {$gt: 88}},

 {_id: 0, title: 1, "tomato.meter": 1, "metacritic": 1})

db.movieDetails.find({$and: [{"metacritic": {$ne: null}},

 {"metacritic": {$exists: true}}]},

 {_id: 0, title: 1, "metacritic": 1})

db.movieDetails.find({$and: [{"metacritic": null},

 {"metacritic": {$exists: true}}]},

 {_id: 0, title: 1, "metacritic": 1})

Lecture: Array Operators: $all

m001_all_operator.js

db.movieDetails.find({genres: {$all: ["Comedy", "Crime", "Drama"]}},

 {_id: 0, title: 1, genres: 1}).pretty()

Lecture: Array Operators: $size

  • In order for a document to match a filter using $all, all the elements we're looking for must be included in that array field.

m001_size_operator.js

db.movieDetails.find({countries: {$size: 1}}).pretty()

Lecture: Array Operators: $elemMatch

  • $elemMatch expects a document as its value, and within that document you specify selectors that will then be used to identify at least one element in this array that matches all of these criteria.

m001_elemMatch_operator.js

boxOffice: [ { "country": "USA", "revenue": 228.4 },

 { "country": "Australia", "revenue": 19.6 },

 { "country": "UK", "revenue": 33.9 },

 { "country": "Germany", "revenue": 16.2 },

 { "country": "France", "revenue": 19.8 } ]

db.movieDetails.find({"boxOffice.country": "Germany", "boxOffice.revenue": {$gt: 17}})

=> 결과없음

db.movieDetails.find({"boxOffice.country": "Germany", "boxOffice.revenue": {$gt: 228}})

=> 결과없음

  

use video

martian = db.movieDetails.findOne({title: "The Martian"})

martian

delete martian._id;

martian

martian.boxOffice = [

 {"country": "USA", "revenue": 228.4},

 {"country": "Australia", "revenue": 19.6},

 {"country": "UK", "revenue": 33.9},

 {"country": "Germany", "revenue": 16.2},

 {"country": "France", "revenue": 19.8}

]

db.movieDetails.insertOne(martian);

  

db.movieDetails.find({boxOffice: {$elemMatch: {"country": "Germany", "revenue": {$gt: 17}}}})

=> 결과없음

db.movieDetails.find({boxOffice: {$elemMatch: {"country": "Germany", "revenue": {$gt: 16}}}})

=> 결과있음

Lecture: $regex Operator

m001_regex_operator.js

db.movieDetails.find({}, {_id: 0, "title": 1, "awards.text": 1}).pretty()

db.movieDetails.find({"awards.text": {$regex: /^Won.* /}}, {_id: 0, title: 1, "awards.text": 1}).pretty()

Challenge Problem: Single Value in an Array of Integers

Final Exam

728x90
반응형