✅MongoDB
1) 고정 되지 않은 테이블 스키마 ( 필요 시 필드ㅡ 추가/ 제거 가능 -> 개발 속도 향상 )
2) 데이터 간의 관계를 정의하지 않는 데이터 베이스
3) 분산형 구조 ( 대용량 데이터 저장 용이 )
➡️ MongoDB structure
1) 데이터베이스 ( database )
mongoDB의 최상위 단위로 하나의 MongoDB 서버에는 여러 개의 database를 만들 수 있다.
각각의 database는 독립적인 공간을 가지며 , 다른 database와 데이터를 공유하지 않는다.
➡️ 컬렉션 (Collection)
RDMS에서의 테이블과 유사한 개념으로 하나의 database 안에는 여러 개의 collection이 존재할 수 있다.
collection은 같은 종류의 document의 집합으로 스키마가 자유롭기 때문에 , 같은 collection 안의 document 구조가 조금 달라도 된다.
➡️ Document
data recode를 BSON (Binary Json)으로 저장한다. 그리고 field(key)는 중복 불가하고 대소문자 구별한다.
📌명령어
show dbs : 전체 database 목록
db : 현재 database 확인
use dbname : 해당 database 로 변경
show collections : 현재 database 의 collection 목록
✅ CRUD
➡️ insert()
db.collection.insertOne( document )
db.collection.insertMany ([
document ,
document ,
documnet ,
...
])
➡️ find()
db.collections.find(query, projection)
// query : query selector
// projection : 출력할 field 결정 ( 1: true / 0 : false )
➡️ cursor
var cursor = db.collection.find()
// find()를 통해 리턴되는 cursor 를 var 변수(js)에 저장할 수 있다.
// hasNext(), forEach() , toArray() 등을 사용하여 cursor 내부의 document들을 사용할 수 있다.
➡️update()
db.collection.updateOne(filter , update , options) // field 수정
db.collection.updateMany(filter , update , options)
db.collection.replaceOne(filter, update, options) // document 수정
//filter : 수정할 document 를 find()
//update : update operatory or aggregation pipeline
//options : 추가적인 기능 (upsert , writeConcern, ...)
➡️delete()
db.collection.deleteOne(filter , options)
db.collection.deleteMany(filter , options)
//filter : 삭제할 document를 find()
//options : 추가적인 기능 (writeConcern , collation)
✅ aggregation
➡️ pipeline
- collection 이 각 stage를 거치면서 document 처리 및 집계
- 일부 처리는 shard 에 대응 (각 shard 에서 처리 )
pipeline : 이전 단계의 연산 결과를 다음 단계에서 사용
stage 순서 중요!!
SQL | NoSQL |
WHERE | $match |
GROUP BY | $group |
HAVING | $match |
SELECT | $project |
ORDER BY | $sort |
LIMIT | $limit |
SUM | $sum |
COUNT | $sum |