블로그

[MongoDB] Collection/Document 생성 및 제거

컬렉션 생성

> db.createCollection("address")
{ "ok" : 1 }

> db.createCollection("address", { capped: true, size: 6142800, max: 30000 })
{ "ok" : 1 }

컬렉션 목록 조회

> show collections
address
articles

컬렉션 삭제

> use mydb
switched to db mydb
> db.articles.drop()
true
> show collections
address

도큐먼트 추가

> db.address.insert({"name": "홍길동", "phone": "010-1234-5678"})
WriteResult({ "nInserted" : 1 })

> db.address.insert([{"name": "홍길동2", "phone": "010-1234-5678"}, {"name": "홍길동3", "phone": "010-1234-5678"}])
BulkWriteResult({
    "writeErrors" : [ ],
    "writeConcernErrors" : [ ],
    "nInserted" : 2,
    "nUpserted" : 0,
    "nMatched" : 0,
    "nModified" : 0,
    "nRemoved" : 0,
    "upserted" : [ ]
})

도큐먼트 목록 조회

> db.address.find()
{ "_id" : ObjectId("5da028dd63e65f7e24c6c593"), "name" : "홍길동", "phone" : "010-1234-5678" }
{ "_id" : ObjectId("5da0292363e65f7e24c6c594"), "name" : "홍길동2", "phone" : "010-1234-5678" }
{ "_id" : ObjectId("5da0292363e65f7e24c6c595"), "name" : "홍길동3", "phone" : "010-1234-5678" }

좀 더 읽기 편한 형식으로 조회

    > db.address.find().pretty()
{
    "_id" : ObjectId("5da028dd63e65f7e24c6c593"),
    "name" : "홍길동",
    "phone" : "010-1234-5678"
}
{
    "_id" : ObjectId("5da0292363e65f7e24c6c595"),
    "name" : "홍길동3",
    "phone" : "010-1234-5678"
}

도큐먼트 삭제

> db.address.remove({"name": "홍길동2"})
WriteResult({ "nRemoved" : 1 })
> db.address.find()
{ "_id" : ObjectId("5da028dd63e65f7e24c6c593"), "name" : "홍길동", "phone" : "010-1234-5678" }
{ "_id" : ObjectId("5da0292363e65f7e24c6c595"), "name" : "홍길동3", "phone" : "010-1234-5678" }