03. MongoDB 라이프사이클
이번 포스팅에서는 다음 코드 예시를 통해 MongoDB의 라이프사이클에 대하여 알아보도록 하겠습니다.
// user DB "foobar"
use foobar
// define user1
var user1 = {"username" : "gchoi"}
user1.relationships = {"friends" : 100, "enemies" : 1}
// define user2
var user2 = {"username" : "tjkwak"}
user2.relationships = {"friends" : 50, "enemies" : 5}
// define user3
var user3 = {"username" : "jmpark"}
user3.relationships = {"friends" : 10, "enemies" : 3}
// define user4
var user4 = {"username" : "jhlee"}
user4.relationships = {"friends" : 1, "enemies" : 300}
// insert users into "users" collection of DB "foobar"
db.users.insert(user1)
db.users.insert(user2)
db.users.insert(user3)
db.users.insert(user4)
// show users collection
db.users.find()
// show db stats
db.stats()
// change username of user4
user4.username = "hskim"
// change the numbner of friednds
user4.relationships.friends = 5
// change the number of enemies
user4.relationships.enemies = 500
db.users.update({"username" : "jhlee"}, user4)
// show users collection
db.users.find()
// drop the collection "users"
db.users.drop()
// drop the current database
db.dropDatabase()
// show database stats - nothing to show
db.stats()
// shutdown the server
use admin
db.shutdownServer()