연습문제

CRUD

Insert & Delete

  • Insert documents into people (lab2 database).

  • Confirm the insertion by db.people.find()

{ “name”: “Kim”, “age”: 21 },
{ “name”: “Lee”, “age”: 22 },
{ “name”: “Jung”, “age”:27},
{ “name”: “Park”, “age”: 27, “skills”: [ “mongodb”, “python”] },
{ “name”: “Choi”, “age”: 22, “score”: 10 }
  • Delete a document whose name is Jung.

  • Using db.collection_name.remove()

Update

exercise 1

Use the people collection in lab2

  • Replace a document that has the name Lee to

    • The name is Lim and the age is 25

  • Update a document that has the name Kim, setting age to 20

  • Remove the skills field in a document that has the name Park

  • Decrease the score field by 2 in a document that has the name Choi

exercise 2

  • Insert the document into posts collection in lab3.

    • {"name" : "Lee", "content" : "Hello I'm Lee"}

  • Add a comments key containing an array.

    • {"comments" : {"name" : "Kim", "content" : "Good posts.", "votes" : 0}}

  • Add multiple comments.

    • {"comments" : {"name" : "Choi", "content" : "How is it going?", "votes" : 1}}

    • {"comments" : {"name" : "David", "content" : "I'm David, What's up?", "votes" : 2}}

    • {"comments" : {"name" : "Jung", "content" : "Glad to hear that", "votes" : 3}}

exercise 3

  • Insert the document into users collection in lab3.

    • {"name" : "Joe", "emails" : ["joe@naver.com", "joe@gmail.com", "joe@yahoo.com"]}

  • Add other addresses using $addToSet to prevent duplicates.

    • {"emails" : "joe@gmail.com"}

    • {"emails" : "joe@hotmail.com"}

exercise 4

Use the users collection in lab3.

  • Remove first element of joe's emails using $pop.

  • Remove the element "joe@naver.com" of joe's emails using $pull.

exercise 5

Use the posts collection in lab3.

  • Set the votes’ value of the second comment of Lee’s post to 2.

  • Set the votes’ value of the Kim’s comment of Lee’s post to 5 using the position operator.

exercise 6

  • Insert documents into people in lab3.

    • {"name" : "Kim", "age" : 21, "profile" : "Hello I’m Kim"},

    • {"name" : "Lee", "age" : 22 , "profile" : "Hello I’m Lee"},

    • {"name" : "Jung", "age" : 22 , "profile" : "Hello I'm Jung"},

    • {"name" : "Park", "age" : 26 , "profile" : "Hello I'm Park"}

  • Set all the documents’ profile value in people collections to “Your account have been hacked!”

Read

exercise 1

Use the people collection in lab3

  • Find the document whose age is 22.

  • Find the document whose name is Lee except the profile

exercise 2

Use the people collection in lab3.

  • Find the documents whose age is between 22 and 26.

  • Find the documents whose name is not Lee.

exercise 3

Use the people collection in lab3

  • Find the documents whose name is "Lee" or "Jung".

  • Find the documents whose name is not "Kim" or "Park" or "Jung".

exercise 4

Import blog.json into the blog collection in lab4

  • Find documents

    • The language includes both Korean and English

  • Display a document slicing two comments

    • The document’s title is Big data

exercise 5

Use the blog collection in lab4

  • Find a document as following conditions

    • The writer’s first name is Ki

    • The language is Korean

  • Find documents that have Kim’s comment

    • Only for an array

    • Regardless of an array

exercise 6

Import sales.json into the sales collection in lab4

  • Print all documents using cursor methods

  • Sorting amount values in ascending order and then _id in descending order

  • Display three documents whose amount is less than 40

  • Skip two documents sorted by amount in descending order

  • Counting documents whose category of item is Home

Last updated