Introduction to MongoDB
1.0 - draft
1.0 - draft
  • MongoDB
  • Getting Started
    • General
      • Database? Collection? Schema?
      • RDBMS? NoSQL?
      • Scale-Up ? Scale - out?
      • JavaScript Object Notation
    • MongoDB
      • 간략한 역사,
    • Manipulating Database
      • MongoShell
      • PyMongo
  • CRUD
    • General
      • Explain()
      • mongoimport
    • Create
      • Mongo Shell
      • PyMongo
      • Exercises
    • Read
      • Mongo Shell
      • PyMongo
      • Exercises
    • Update
      • Mongo Shell
      • PyMongo
      • Exercises
    • Delete
      • PyMongo
      • Exercises
    • Challenging Exercises
      • Exercise 1
  • Index with Geospatial & Text
    • Basic Index
      • Syntax
      • Exercises
    • Geospatial Index
      • Syntax
      • Exercises
    • Text Index
      • Syntax
      • Exercises
    • Challenge
  • Aggregate framework
    • Aggregate Framework
      • Basic
      • Exercises
  • Distributed
    • Replication
      • Setup
        • Configuration File
        • Running Servers
        • Initiation
        • Exercises
      • Concerns
        • Read Concern
        • Write Concern
        • Exercises
    • Sharding
      • Getting Started
      • Setup
  • MongoDB 4.0 Features
    • Multi-Document Transaction
      • Transaction?
      • Basic
      • Exercises
  • Administration
    • Untitled
Powered by GitBook
On this page

Was this helpful?

  1. CRUD
  2. Challenging Exercises

Exercise 1

Insert (Insert_one, Insert_many), Update, Delete

  • Use a ex1 database and imdb collection.

  • We are going to make a movie database like IMDb.

  • Each movie has following information.

    • title: String

    • director: String

    • genre: List(String)

    • score: Double

  • Insert a movie

    • Write a script that inserts movie information to a movie collection.

    • Type the movie information as follows:

    • Movie Title: Avengers: Infinity War

      Director: Anthony Russo and Joe Russo

      Genre: Action, Adventure, Drama

      score: 8.6

      [INFO] Success to insert!

    • Plain: Terminal output

    • Bold: User input

    • Hint: use a input() function

  • Confirm that the insertion is operated properly.

  • Print only movie’s title(s)

[INFO] Movie list
Avengers: Infinity War

  • Insert three movies more. Use insert_many

    • Movie Title: Bohemian Rhapsody

      Director: Bryan Singer

      Genre: Biography, Drama, Music

      score: 8.1

      Quit (Y/n)? n

    • Movie Title: Happy Death Day 2U

      Director: Christopher Landon

      Genre: Drama, Horror, Mystery

      score: 6.6

      Quit (Y/n)? n

    • Movie Title: Spider-Man: Into the Spider-Verse

      Director: Bob Persichetti, Peter Ramsey

      Genre: Animation, Action, Adventure

      score: 8.6

      Quit (Y/n)? Y

      [INFO] Success to insert!

  • Confirm that the insertion is operated properly.

  • Print only movie’s title(s)

[INFO] Movie list
Avengers: Infinity War
Bohemian Rhapsody
Happy Death Day 2U
Spider-Man: Into the Spider-Verse 

  • Update movie information

    • Querying movie title and update the information

    • [INFO] Enter the information which you want to edit

      Movie Title: Avengers: Infinity War

      Director: Anthony Russo and Joe Russo

      Genre: Action, Adventure, Drama

      score: 8.6

      [INFO] Success to update!

    • Plain: Terminal output

    • Bold: query

    • Italic: update

  • Print all documents

[INFO] Movie list
[{'_id': ObjectId('5c76acb565d1016cd33d4a5d'),
  'director': 'Anthony Russo and Joe Russo',
  'genre': ['Action', ' Adventure', ' Drama'],
  'score': 8.6,
  'title': 'Avengers: Infinity War'},
 {'_id': ObjectId('5c76ad0d65d1016cd33d4a5e'),
  'director': 'Bryan Singer',
  'genre': ['Biography', ' Drama', ' Music '],
  'score': 8.1,
  'title': 'Bohemian Rhapsody'},
 {'_id': ObjectId('5c76ad0d65d1016cd33d4a5f'),
  'director': 'Christopher Landon',
  'genre': ['Drama', ' Horror', ' Mystery'],
  'score': 6.6,
  'title': 'Happy Death Day 2U'},
 {'_id': ObjectId('5c76ad0d65d1016cd33d4a60'),
  'director': 'Bob Persichetti, Peter Ramsey',
  'genre': ['Animation', ' Action', ' Adventure'],
  'score': 8.6,
  'title': 'Spider-Man: Into the Spider-Verse'}]
  • Remove movie (Plain: Output, Bold: User input)

  • Remove a document

    • [INFO] Enter the title which you want to remove

      Movie Title: Happy Death Day 2U

      [INFO] Are you sure? (Y/n) Y

      [INFO] Deleted

  • Result

[INFO] Movie list
Avengers: Infinity War
Bohemian Rhapsody
Spider-Man: Into the Spider-Verse 
PreviousChallenging ExercisesNextBasic Index

Last updated 6 years ago

Was this helpful?