본문 바로가기
Database/MongoDB

Databases, Collections, Documents

by mansoorrr 2023. 9. 20.

1. Overview

MongoDB는 데이터를 documents에 저장하고 documents는 collection안에 저장된다. 이 collection들은 다시 db에 저장된다.

 

2. Databases

db를 사용하기 위해서는 db를 선택해야 한다. db를 선택하기 위해 사용하는 쿼리는 use <db>이다. 만약 db가 없는 상황에서 쿼리를 사용하게 되면 db가 생성된다.

#----- 해당쿼리 작성시
use myDB

#------ 결과
1. myDB라는 db가 있을때: myDB를 선택하게 됨
2. myDB라는 db가 없을때: myDB라는 db가 생성되고 선택됨

 

3. Collections

documents는 collection에 저장된다. collection은 RDBMS에서 Table에 해당된다.

가. collection 생성

collection생성의 경우 두가지 방법이 있다. 명시적으로 생성하는 방법과 자동생성이다. 특정 옵션을 통해 collection을 생성할 경우가 아닐 경우 명시적 생성은 사용할 이유가 없다.

 

1) 명시적생성

#----- newcollection이라는 collection을 생성할 경우
db.createCollection("newcollection")

 

2) 자체생성

insertOne(<document>)과 createIndex(<document>) 오퍼레이션을 사용해 document와 collection을 한번에 생성가능하다.

#------ newcollection을 생성하고 싶은 경우
1. db.newcollection.insertOne({ x: 1 })
2. db.newcollection.createIndex({ y: 1 })

collection의 이름을 지을때 주의해야 한다. 특수문자 금지, 길이제한, 소문자와 대문자구분 등이 있는데, 이와 관련된 사항은 아래 링크에서 확인 가능하다.

 

MongoDB Limits and Thresholds — MongoDB Manual

Docs Home → MongoDB Manual This document provides a collection of hard and soft limitations of the MongoDB system.BSON Document SizeThe maximum BSON document size is 16 megabytes.The maximum document size helps ensure that a single document cannot use ex

www.mongodb.com

 

4. Documents

MongoDB는 데이터를 BSON document로 저장하는데 이는 JSON document라고 생각하면 된다. 위의 그림처럼 document는 field와 value쌍으로 이루어진 구조이다. 아래 코드에서 field의 value는 BSON  데이터 타입을 갖는다. field에 대한 설명은 다음과 같다.

var mydoc = {
               _id: ObjectId("5099803df3f4948bd2f98391"),
               name: { first: "Alan", last: "Turing" },
               birth: new Date('Jun 23, 1912'),
               death: new Date('Jun 07, 1954'),
               contribs: [ "Turing machine", "Turing test", "Turingery" ],
               views : NumberLong(1250000)
            }
필드명 data type
_id ObjectId
name embedded document
birth Date
death Date
contribs array
views NumberLong

 

가. 필드이름

필드 이름은 다음과 같은 특성을 갖는다.

  • 필드이름은 문자이다.
  • _id값은 primary key로 유일해야 한다.
  • 필드 이름은 null이 포함될 수 없다.
  • 한 collection에 중복된 필드명을 사용할 수 없다

 

나. 점표기법(Dot Notation)

데이터를 불러오고 싶을때는 '.'을 이용해 데이터를 불러온다. 불러오는 방법에는 2가지가 있다.

 

1) array

array에서 특정 순서에 있는 데이터를 불러오고 싶다면 '.'을 이용한다. array의 제일 앞의 index는 0이다.

#------ 해당 데이터에서 Turignery를 불러오고 싶다면
{
   ...
   contribs: [ "Turing machine", "Turing test", "Turingery" ],
   ...
}

#------ 결과(아래와 같이 입력한다)
contribs.2

 

2) embedded documents

필드가 document로 이루어져 있다면 아래와 같이 사용한다.

#----- 해당 document에서 name의 Turing을 가져오고 싶다면
{
   ...
   name: { first: "Alan", last: "Turing" },
   contact: { phone: { type: "cell", number: "111-222-3333" } },
   ...
}

#----- 결과(아래와 같이 입력한다)
"name.last"


#----- 해당 document에서 contact의 111-222-3333을 가져오고 싶다면
{
   ...
   name: { first: "Alan", last: "Turing" },
   contact: { phone: { type: "cell", number: "111-222-3333" } },
   ...
}

#----- 결과(아래와 같이 입력한다)
"contact.phone.number"

 

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

CRUD Operations  (0) 2023.09.25
Intro  (0) 2023.09.20