ABOUT ME

-

  • MongoDB) MongoDB 소개와 간단한 사용법
    MongoDB 2021. 1. 13. 22:50

     

    MongoDB

    • NoSQL 기반의 데이터베이스입니다.
    • NoSQL의 의미는 SQL이 없다라는 뜻이 아닌 Not Only SQL입니다. 기존의 RDMS(관계형 데이터베이스)의 한계를 극복하기 위해 만들어진 데이터저장소입니다.
    • Schema-less(Schema가 없습니다.)
    • Application에서 사용되는 객체를 데이터베이스에 추가할 때 Conversion/Mapping이 불필요합니다.

    설치과정은 생략합니다.


    Collection

    Document의 그룹입니다. RDMS의 Table과 비슷한 개념이지만, schema를 가지고 있지 않습니다. 

    Document들이 동적인 schema를 가지고 있기 때문입니다.

    Document

    {
        "_id": ObjectId("5099803df3f4948bd2f98391"),
        "username": "velopert",
        "name": { first: "M.J.", last: "Kim" }
    }

    key-value로 이뤄진 구조입니다. RDMS의 Tuble/Row와 같은 개념입니다.


    MongoDB 명령어

    DataBase

    mongo 명령어를 통해  mongodb를 시작합니다.

    > mongo

     

    use 명령어를 통해 DB를 생성합니다. DB가 없는 경우 생성이 되고, 이미 존재하는 경우 현존하는 DB를 사용합니다.

    mongodb_tutorial이라는 DB를 생성합니다.

    > use mongodb_tutorial
    switched to db mongodb_tutorial
    현재 사용중인 db확인은 db 명령어를 입력하시면 됩니다.
    > db
    mongodb_tutorial
    db 리스트를 확인하려면 show dbs 명령어를 입력합니다.
    > show dbs
    local  0.000GB

    방금 만든 mongodb_tutorial이 db 리스트에 보이지 않는 이유는 Document들이 한개도 없기 때문입니다.

    한개 이상의 Document가 존재한다면 리스트에 보이게 됩니다.

     

    db.dropDatabase()

    db 삭제 명령어입니다. use명령어로 먼저 db를 선택해줘야 합니다.

    > use mongodb_tutorial
    switched to db mongodb_tutorial
    > db.dropDatabase();
    { "dropped" : "mongodb_tutorial", "ok" : 1 }

    Collection

     

    db.createCollection()

    use 명령어로 db를 선택 후 collection을 생성합니다.

    > use test
    switched to db test
    > db.createCollection("books")
    { "ok" : 1 }
    db.collection.insert()
    > db.people.insert({"name": "velopert"})
    WriteResult({ "nInserted" : 1 })

    위의 방법처럼 document를 추가하면 자동으로 collection을 생성할 수 있습니다.

     

    show collections

    collection 리스트 확인 명령어입니다.

     

    db.collection_name.drop()

    collection 제거 명령어입니다.

    > use test
    switched to db test
    > show collections
    articles
    books
    people
    > db.people.drop()
    true
    > show collections
    articles
    books

    Document

     

    insert()

    insert() 명령어로 document를 추가할 수 있습니다. 1개부터 n개까지 동시에 추가 가능합니다.

    // single
    > db.books.insert({"name": "NodeJS Guide", "author": "Velopert"})
    WriteResult({ "nInserted" : 1 })
    
    // multy
    > db.books.insert([
    ... {"name": "Book1", "author": "Velopert"},
    ... {"name": "Book2", "author": "Velopert"}
    ... ]);
    BulkWriteResult({
            "writeErrors" : [ ],
            "writeConcernErrors" : [ ],
            "nInserted" : 2,
            "nUpserted" : 0,
            "nMatched" : 0,
            "nModified" : 0,
            "nRemoved" : 0,
            "upserted" : [ ]
    })
    db.collection_name.find()

    document 리스트 확인 명령어입니다.

     

    db.collection_name.remove("key","value")

    document 제거 명령어입니다.

    > db.books.find({"name": "Book1"})
    { "_id" : ObjectId("56c097f94d6b67aafdeb88ac"), "name" : "Book1", "author" : "Velopert" }
    > db.books.remove({"name": "Book1"})
    WriteResult({ "nRemoved" : 1 })
    db.collection_name.find().pretty()

    document를 깔끔한 json형식으로 보게해주는 명령어입니다.

    > db.articles.find().pretty()
    {
            "_id" : ObjectId("56c0ab6c639be5292edab0c4"),
            "title" : "article01",
            "content" : "content01",
            "writer" : "Velopert",
            "likes" : 0,
            "comments" : [ ]
    }

    Reference

    MongoDB 강좌

    반응형

    댓글

Designed by Me.