MongoDB의 CRUD(Create-Read-Update-Delete) Operation에 대해 살펴본다.
1. Create
- db.collection.insertOne() : 하나의 document 생성
- db.collection.insertMany(): 다수의 document 생성
2. Read
- db.collection.find(): 하나 또는 다수의 document를 찾는다.
인자로 쿼리를 작성해 document를 찾는다.
값을 넣어주지 않으면 모든 document를 출력한다.
ex) address의 필드 값이 "Seoul" 인 document 찾기
ex) age가 20이상인 document 찾기
3. Update
- db.collection.updateOne(): 하나의 document 수정, 조건에 해당되는 document가 많으면 가장 처음 발견되는 document를 수정한다.
- db.collection.updateMany(): 다수의 document 수정
- db.collection.replaceOne(): 조건에 해당하는 document 교체
ex) age가 1 초과인 document 의 address 필드 값을 "Seoul" 로 변경
4. Delete
- db.collection.deleteOne(): 하나의 document 삭제, 조건에 해당되는 document가 많으면 가장 처음 발견되는 document 삭제
- db.collection.deleteMany(): 다수의 document 삭제
- db.collection.deleteMany({}) 는 모든 document를 삭제한다.
ex) address의 필드 값이 "Busan" 인 document 하나를 삭제
'MongoDB' 카테고리의 다른 글
Mongoose (0) | 2023.04.10 |
---|---|
MongoDB 설치하기 - Mac M1 (0) | 2023.04.07 |
MongoDB (0) | 2023.04.07 |