본문 바로가기

DB/MongoDB

[MongoDB] 5. MongoDB 시작

728x90
반응형

MongoDB Shell은 "자바 스크립트" 문법을 따름.

 

기본 명령어

db // 현재 선택된 데이터베이스 이름
show dbs // 데이터베이스 목록 확인
show collections // 컬렉션 목록 확인
use testDB // testDB ” 데이터베이스 선택 및 생성
db. collection_name.drop // 컬렉션 삭제
(특정 도큐먼트를 삭제하려면 조건을 명시해야 함 .

 

DB 생성

  • 이렇게 초기 콘솔에서 db 이름을 입력하거나 use를 사용해 생성함.

 

Collection 생성

db.createCollection("컬렉션 이름")

 

Doucment 생성

  • insertOne() : 1개의 Document 삽입
// db.컬렉션이름.insertOne(1 Document)
db.myCollection.insertOne(
	{
	name:"sue",
	height:165,
	address:"Mokpo"
	}
)

  • insertMany() : 여러개의 Document 삽입
// db.컬렉션.insertMany([{도큐먼트1},{도큐먼트2},{도큐먼트3},..])
db.msg.insertMany([
	{"To":"Jung","From":"홍길동","Comments":"My name is Hong"},
	{"To":"Jung","From":"홍길동","Comments":"What's your name?"},
	{"To":"홍길동","From":"Jung","Comments":"My name is jung"},
	{"To":"홍길동","From":"Jung","Comments":"bye"}
])

 

Document 검색 : find()

db.컬렉션.find() // 컬렉션 도큐먼트 전체 검색
db.msg.find()

컬렉션 EXPORT 하기(내보내기)

  • Tools가 설치되어 있고 폴더에 mongoexport 파일 설치 확인하기
  • 시스템 환경변수 path에 경로 추가되어 있어야 합니다.

cmd 실행 후 다음 명령문 실행

mongoexport --collection = 내보낼 컬렉션 이름 --db = 내보낼 컬렉션이 포함된 DB이름 --port = 27017 --out=내보낼 파일 이름.json
mongoexport --collection=book --db=myPhone --port=27017 --out=myfile.json

  • 파일 저장 경로는 cmd가 열린 폴더에 저장됨.

컬렉션 IMPORT 하기(불러오기)

  • Tools가 설치되어 있고 폴더에 mongoimport 파일 설치 확인하기
  • 시스템 환경변수 path에 경로 추가되어 있어야 합니다.

컬렉션이 저장되는 폴더(MongoDB\Server\6.0\data)에 import할 json 파일 추가

cmd 실행 후 MongoDB\Server\6.0\data 위치로 이동 후 다음 명령문 실행

mongoimport -c 저장할 컬렉션 이름 --file 파일이름.json
mongoimport -c myfile --file myfile.json

728x90
반응형

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

[MongoDB] 7. 기본 명령어 - replace, update  (0) 2022.09.29
[MongoDB] 6. 기본 명령어 - find()  (0) 2022.09.29
[MongoDB] 4. MongoDB 환경  (0) 2022.09.29
[MongoDB] 3. MongoDB란?  (0) 2022.09.29
[MongoDB] 2. JSON  (0) 2022.09.29