본문 바로가기
IT/MongoDB

[10일차] MongoDB

by GWLEE 2022. 7. 1.

 

 

db.movies.insertMany([
    {"title" : "Ghostbusters"},
    {"title" : "E.T"},
    {"title" : "Blade Runner"}
], {"ordered": true}) // 디폴트로 있는 것 그래서 기본 순서대로 들어감

db.movies.find()
db.movies.drop()

db.movies.insertMany([
        {"_id" : NumberInt(0), "title" : "Top Gun"},
        {"_id" : NumberInt(1), "title" : "Back to the Future"},
        {"_id" : NumberInt(1), "title" : "Gremlins"}, // pk 중복 일부러 지정 
        {"_id" : NumberInt(2), "title" : "Aliens"},
])

db.movies.insertMany([
        {"_id" : 3, "title" : "Sixteen Candies"},
        {"_id" : 4, "title" : "The Terminatator"},
        {"_id" : 4, "title" : "The Princess Bride"},
        {"_id" : 5, "title" : "Scarface"},
], {"ordered" : false})


db.movies.deleteOne({"_id": 4}) // 하나 지울때
db.movies.find()
db.movies.drop()

db.movies.insertMany([ { "_id" : NumberInt("0"), "title" : "Top Gun", "year" : 1986 },
 { "_id" : NumberInt("1"), "title" : "Back to the Future", "year" : 1985 },
{ "_id" : NumberInt("3"), "title" : "Sixteen Candles", "year" : 1984 }, 
{ "_id" : NumberInt("4"), "title" : "The Terminator", "year" : 1984 },
{ "_id" : NumberInt("5"), "title" : "Scarface", "year" : 1983 }])

db.movies.find()

db.movies.deleteMany({"year": 1984}) // 여러개 지울때 
db.movies.find()
db.movies.deleteMany({}) // 다 지운거
db.movies.find()

db.users.drop()
db.users.insertOne({"name": "joe", "friends": 32, "enemies": 2})
db.users.find()

//replace
joe = db.users.findOne({"name": "joe"})
joe.relationships = {"friends": joe.friends, "enemies": joe.enemies}
joe.username = joe.name
delete joe.friends
delete joe.enemies
delete joe.name
db.users.replaceOne({"name": "joe"}, joe)

//update 특정필드갱신
db.analytics.insertOne({"url" : "www.example.com", "pageviews" : 52})
db.analytics.findOne()

db.analytics.updateOne({"url" : "www.example.com"}, {"$inc":{"pageviews":1}})

//set 연산자
db.users.insertOne({"name": "joe", "age":NumberInt("30"), "gender" : "male"})
db.users.find()

// 필드값 생성
db.users.updateOne({"_id": ObjectId("62be8ab0e70a5b787ab342dd")}, {"$set" : {"favorite book" : "War and Peace"}})

// 필드 값 업데이트 set은 있으면 업데이트 해준다! 없으면 생성
db.users.updateOne({"_id": ObjectId("62be8ab0e70a5b787ab342dd")}, {"$set" : {"favorite book" : "Hello"}})

// 필드 값 업데이트
db.users.updateOne({"_id": ObjectId("62be8ab0e70a5b787ab342dd")}, 
{"$set" : {"favorite book" : ["Cat's Cradle", "Foundation Trilogy", "Ender's Game"]}})

//favorite book 삭제 
db.users.updateOne({"_id" : ObjectId("62be8ab0e70a5b787ab342dd")}, {"$unset" : {"favorite book" : 1}})

db.blog.posts.insertOne( {"title" : "A Blog Post", "content" : "...", "author" : { "name" : "joe", "email" : "joe@example.com" }})
db.blog.posts.find()

db.blog.posts.updateOne({"author.name" : "joe"},
{"$set": {"author.name" : "joe.schmoe"}})

db.games.insertOne({"game": "pinball", "user" : "joe"})
db.games.find()

db.games.updateOne({"game": "pinball", "user" : "joe"},
{"$inc" : {"score" : 50}})


db.games.updateOne({"game": "pinball", "user" : "joe"},
{"$inc" : {"score" : 100}})

// 만약 없으면 array 생성

db.blog.posts.drop()

db.blog.posts.insertMany([ {"title" : "A blog post", "content" : "..."}, {"title" : "Notice", "content" : "Welcome!!"}])
db.blog.posts.find()

db.blog.posts.updateOne({"title" : "A blog post"},
{"$push" : {"comments" : {"name" : "joe", "email" : "joe@example.com", "content" : "nice post."}}}
) // 배열로 생성됨

// 두 번째 배열로 들어감
db.blog.posts.updateOne({"title" : "A blog post"},
{"$push" : {"comments" : {"name" : "bob", "email" : "bob@naver.com", "content" : "good!!"}}}
)

// 배열의 항목 여러개 붙임.
db.stock.insertOne({"_id" : "KOSDAQ", "desc" : "Korean Market"})
db.stock.find()
db.stock.updateOne({"_id" : "KOSDAQ"},
{"$push" : {"hourly" : {"$each": [562.776, 562.790, 559.123]}}}
)

db.movies.insertOne({"genre" : "horror", "top10" : [
{"name": "The Mist", "rating": 4.3},
{"name": "The Birds", "rating": 5.2},
{"name": "Dawn of the Dead", "rating": 3.6},
{"name":  "Shaun of the Dead", "rating": 6.1},
{"name": "Evil Dead2", "rating": 7.8},
{"name": "The Babadook", "rating": 7.1},
{"name": "The Cabin in the Woods", "rating": 8.4},
{"name": "A Quiet Place", "rating": 8.2},
{"name": "Paranormal Activity", "rating": 8.7},
{"name": "The Descent", "rating": 8.8}
]})
db.movies.find()

db.movies.updateOne({"genre" : "horror"},
{"$push" : {"top10" : {"$each" : [
                {"name": "Nightmare on Elm Street", "rating": 9.1},
                {"name": "Saw", "rating": 9.9}],
            "$slice" : -10}}}
 )

db.movies.updateOne({"genre" : "horror"}, 
{"$push" : {"top10" : 
    {"$each" : [{"name" : "It Follows",  "rating" : 9.8}, 
                {"name" : "Rec", "rating" : 8.3}], 
    "$slice" : 10,  "$sort" : {"rating" : -1}}
    }
})


db.products.drop()
db.products.insertOne({"product name": "Television"})
db.products.updateOne({"product name": "Television"},
    {$push : {"product color" : "Red"}})
db.products.updateOne({"product name": "Television"},
    {$push : {"product color" : "Blue"}})
db.products.find()

// red가 없으면 넣어라..
db.products.updateOne({"product color" : {"$ne" : "Red"}}, {$push : {"product color" : "Red"}})

db.users.drop()
db.users.insertOne({"username" : "joe", 
    "emails" : [ "joe@example.com", "joe@naver.com", "joe@gmail.com"]
})
db.users.find()

db.users.updateOne({"username" : "joe"}, {"$addToSet" : {"emails" : "joe@naver.com"}})

// 추가
db.users.updateOne({"username" : "joe"}, {"$addToSet" : {"emails" : "joe@daum.net"}})

// 여러 개 항목  넣기
db.users.updateOne({"username" : "joe"}, {"$addToSet" : {"emails" :
   {"$each" : ["joe@naver.com", "joe@gmail.com", "joe@bit.com"]}
   }})

// 위치 기반 pop.. 맨 뒤에 있는 bit가 사라짐
db.users.updateOne({"username" : "joe"}, {"$pop" : {"emails" : 1}})
db.users.find()
db.users.updateOne({"username" : "joe"}, {"$pop" : {"emails" : -1}})

// 항목 
db.lists.insertOne({"todo" : ["dishes", "laundry", "gardening"]})
db.lists.find()

// laundry 삭제
db.lists.updateOne({}, {"$pull" : {"todo" : "laundry" }})

// 배열 내 데이터 삭제
db.blog.posts.drop()
db.blog.posts.insertOne({"content" : "...",
        "comments" : [
        {"comment" : "good post", "author" : "John", "votes" : NumberInt("0")},
        {"comment" : "I thought it was too short", "author" : "Claire", "votes" : NumberInt("3")},
        {"comment" : "free watches", "author" : "Alice", "votes" : NumberInt("-5")},
        {"comment" : "vacation getaways", "author" : "Lynn", "votes" : NumberInt("-7")}]})
db.blog.posts.find()

// comments 배열에 0번째 votes 1을 증가 시킨다.
db.blog.posts.updateOne({"comments.author" : "John"}, {"$inc" : 
        {"comments.0.votes" : NumberInt("1")}
    }
 )
 
 // 작성자 이름 바꾸기
 db.blog.posts.updateOne({"comments.author" : "John"},
  {"$set" : {"comments.$.author" : "Jim"}
  }
)

// 
db.blog.posts.updateOne({}, // 모든 것을 대상 
    {"$set" : {"comments.$[elems].hidden" : true}}, // hidden에 넣기
    {arrayFilters : [ {"elems.votes" : { "$lte" : -5}}] } ) // vote가 5보다 작은걸 끄집어 내기
 
db.blog.posts.find()






'IT > MongoDB' 카테고리의 다른 글

[12일차] MongoDB 정리본  (0) 2022.07.05
[12일차] MongoDB  (0) 2022.07.05
[11일차] MongoDB 정리본  (0) 2022.07.04
[11일차] Studio 3T for MongoDB  (0) 2022.07.04
[10일차] MongoDB 정리본  (0) 2022.07.01

댓글