04-19 05:22
Notice
Recent Posts
Recent Comments
관리 메뉴

Scientific Computing & Data Science

[MongoDB] Tips / DB 및 Collection 삭제 명령어 본문

Data Science/MongoDB

[MongoDB] Tips / DB 및 Collection 삭제 명령어

cinema4dr12 2015. 4. 19. 23:14

MongoDB의 Database 및 Collection을 삭제하는 방법에 대해 알아보자.

collection 삭제

현재 db에 "users"라는 이름의 collection이 있다고 가정하자.


> db.getCollectionNames()
[ "system.indexes", "users" ]


현재 db에서 users collection을 삭제하는 명령은 drop이다.


> db.users.drop()
true
> db.getCollectionNames()
[ "system.indexes" ]


drop 명령을 통해 collection이 삭제된 것을 확인할 수 있다.

db 삭제

먼저 "test"라는 이름의 db를 생성한다:


> use test
switched to db test
> show dbs
local  0.078GB
test   0.078GB


db를 삭제하는 명령어는 dropDatabase이다.


> db.dropDatabase()
{ "dropped" : "test", "ok" : 1 }
> show dbs
local  0.078GB

collection 내용 삭제

해당 collection의 내용을 전체 삭제하는 명령어는 remove이다:


> use test
switched to db test
> db.users.insert({username: "gchoi"})
WriteResult({ "nInserted" : 1 })
> db.users.insert({username: "jmpark"})
WriteResult({ "nInserted" : 1 })
> db.users.insert({username: "hskim"})
WriteResult({ "nInserted" : 1 })
> db.users.insert({username: "tjkwak"})
WriteResult({ "nInserted" : 1 })
>
> db.users.find().pretty()
{ "_id" : ObjectId("5533b7e365ab6551bc5be2a7"), "username" : "gchoi" }
{ "_id" : ObjectId("5533b7ea65ab6551bc5be2a8"), "username" : "jmpark" }
{ "_id" : ObjectId("5533b7ef65ab6551bc5be2a9"), "username" : "hskim" }
{ "_id" : ObjectId("5533b7f465ab6551bc5be2aa"), "username" : "tjkwak" }
>
> db.users.remove({})
WriteResult({ "nRemoved" : 4 })
> db.users.find().pretty()
>


Comments