MongoDB의 기본 사용법 정리
태그: Mongo
카테고리: Database
업데이트:
클라이언트 실행하기
mongo
Mongo DB 기본 사용법
Database 관련
데이터베이스 목록 보기
show dbs
현재 사용중인 db 이름 보기
db
사용중인 db 변경하기 (단, db가 존재하지 않으면 새로 생성 후 변경)
use [데이터베이스 이름]
Collection 관련
존재하는 collection 목록 보기
show collections
collection 생성하기
db.createCollection("콜렉션 이름");
collection 제거
db.[콜렉션 이름].drop()
Document 관련
Document 삽입
db.[콜렉션 이름].insertOne(document1)
db.[콜렉션 이름].insertMany([document1, ... ])
모든 Document 보기
db.[콜렉션 이름].find()
조건에 맞는 Document 보기
여기서 필터는 {필드 이름 : 값} 형태로 삽입합니다.
db.[콜렉션 이름].find({필터})
db.myCollection.find({name: "TestName"})
Document 업데이트
updateOne은 필터에 맞는 document중 처음 발견한 1개만 update합니다. updateMany은 필터에 맞는 document 모두 update합니다.
db.[콜렉션 이름].updateOne({필터}, {$set: {필드이름: 값}})
db.[콜렉션 이름].updateMany({필터}, {$set: {필드이름: 값}})
Document 제거
db.[콜렉션 이름].remove({})
댓글남기기