Skip to content

Instantly share code, notes, and snippets.

@AndersonChoi
Last active October 30, 2018 06:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndersonChoi/e4e00ec56e10ae271f21586bdfeee6ae to your computer and use it in GitHub Desktop.
Save AndersonChoi/e4e00ec56e10ae271f21586bdfeee6ae to your computer and use it in GitHub Desktop.
MongoDB getting started

MongoDB

Introduce this document

C++로 작성된 오픈소스 문서지향적 Cross-platform 데이터베이스인 mongodb의 명령어를 모은 document입니다.

Database 조회/생성/삭제

DB 생성하기

$ use testdb
$ db.testdb.insert({"test_name":"noname", "test_numebr":1234})

DB 확인하기

$ show dbs

현재 사용중인 DB 확인

$ db

DB 제거하기

$ use testdb
$ db.dropDatabase();

Collection 조회/생성/삭제

Collection 조회하기

$ show collections

Collection 생성하기-1

$ db.testdb.insert({"test_name":"noname", "test_numebr":1234})

Collection 생성하기-2

$ db.createCollection("test_collection")

Collection을 옵션과 함께 생성하기

$ db.createCollection("People",{capped:false, max:10000})

Collection 제거

$ db.test_collection.drop()

Document 조회/생성/삭제/

모든 Document 조회

$ db.testdb.find()

특정 Document 조회

$ db.testdb.find({"age":25})

Document 삭제

$ db.testdb.deleteOne({"status":"done"}) // 조건에 맞는 document 하나 삭제
$ db.testdb.deleteMany({"age":26}) // 조건에 맞는 document 모두 삭제

Document 생성

$ db.testdb.insert([{"name":"John",  "age":24}, {"name":"Jane", "age":26}])

Document 수정

$ db.testdb.update({"name":"John"},{$set: {"age":30}})

Query문 사용

$eq (equals) 주어진 값과 일치하는 값

$gt (greater than) 주어진 값보다 큰 값

$gte (greather than or equals) 주어진 값보다 크거나 같은 값

$lt (less than) 주어진 값보다 작은 값

$lte (less than or equals) 주어진 값보다 작거나 같은 값

$ne (not equal) 주어진 값과 일치하지 않는 값

$in 주어진 배열 안에 속하는 값

$nin 주어빈 배열 안에 속하지 않는 값

$or 논리연산자 or

$and 논리연산자 and

age가 10초과 20미만 조회

$db.testdb.find({"age":{$gt:10, $lt:60}})

주소가 pusan, seoul에 속하는 document 조회

$db.testdb.find({"address":{$in:["pusan","seoul"]}})

조회결과에서 선택적인 element만 조회

$db.testdb.find({},{"name":true,"age":false,_id:false})
{"name":Jane}
{"name":John}

오름차순(1), 내림차순(-1) 조회

$db.testdb.find({},{"name":false,"age":true,_id:false}).'''sort({age:-1})'''
{"age":80}
{"age":55}
{"age":21}

Document n개 조회

$db.testdb.find().limit(3)

처음 n개 데이터 생략하여 조회

$db.testdb.find().skip(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment