MongoDB CLI

위 cli는 MongoDB의 Shell을 이용하여 Cloud MongoDB에 접속할때 사용하는 cli입니다.

Mongo Shell 설치 및 사용법 링크

 현재 존재하는 DataBase 확인하기

해당 MongoDB에 있는 DataBase를 출력합니다.

$ show dbs


DataBase생성

DataBase을 생성과 동시에 사용합니다.

$ use [DataBase Name]


DataBase안에서 Collection을 생성

DB가 선택되어 있는 상태에서 Collection을 생성합니다.(Table과 개념이 비슷함)

Collection의 구조도 설정할수 있습니다. 링크참조

$ db.createCollection("CollectionName")


Collection안에 document생성

Collection안에 document를 생성한다. 원소는 JSON타입이다. (Record과 개념이 비슷함)

$ db.testCollection.insert({[Document생성]});


MongoDB에서 다른 DB로 Collections Copy하기

복사할 DB를 사용하고 있어야 합니다(use)

위치: 복사원본 DB

사용하고 있는 DB의 collection을 다른 DB로 복사합니다. (존재하는 모든 documents)

$ db.[복사원본 Collection].find().forEach(function(d){db.getSiblingDB([카피할DB - string])[카피할DB의 Collection - string].insert(d);});


MongoDB에서 Document갯수 세기

현재 DB의 Collection안에 관련 Documents가 몇개 있는지 반환합니다. (count제거시 배열로 변환)

$ db.[collection_name].find({key입력}).count()


MongoDB에서 해당 collection의 Documents를 모두 삭제하기

현재 DB의 Collection안에 관련 Documents를 모두 삭제합니다. (주의: 원복이 불가능함)

$ db.[collection_name].remove({})


MongoDB에서 Document갯수 세기(중복 제거)

현재 DB의 Collection안에 관련 Documents가 몇개 있는지 반환합니다. (length 제거시 배열로 변환)

Value값이 복수로 중복되어 있다면 1개로 판단합니다. {}안에 키값만 입력하면 키값(문자열)을 기준으로 중복을 제거하고 count합니다.

$ db.[collection_name].distinct({key값만 입력}).length


댓글

이 블로그의 인기 게시물

Lesson 12_1 프로퍼티 노드(Property Node)

DAQ로 전압 측정하기-2

Lesson 12_2 참조를 이용한 프로퍼티노드(Property Node)