본문 바로가기
카테고리 없음

MongoDB 명령어

by 별빛과하엘 2020. 12. 24.

 

mongodb를 실행하는법

# 몽고디비 실행하기 
# --dbpath 옵션을 사용해 데이터가 저장될 저장소를 지정합니다. 
mongod --dbpath path/to/data_store

 

 

"" 부분은 제거하고 입력하시면 됩니다.

-외부에서나 내부에서 접속 명령어-

mongo -u "아이디" -p "패스워드" "외,내부아이피+/해당데이터베이스 이름"

ex) mongo -u test -p password localhost:27017/test

 

 

 

 

 

들어가고나면 mongodb 쉘로 들어가게 되는데 그다음 명령어는

 

: 뒤로는 쉘에서 입력하시면 안됩니다.!!

 

Database

데이터베이스 명령어

>db : 현재 사용중인 데이터베이스 확인
>use 데이터베이스명 : 데이터베이스 생성
>show dbs : 데이터베이스 리스트 확인
>db.stats() : 데이터베이스 상태확인
>db.dropDatabase() : 데이터베이스 삭제

 

 

 

 

Collection( SQL 에서 테이블과 비슷한 개념!!)

컬렉션 명령어

컬렉션 생성 명령어 입니다.
db.createCollection( <name>,
   {
     capped: <boolean>,
     autoIndexId: <boolean>,
     size: <number>,
     max: <number>,
     storageEngine: <document>,
     validator: <document>,
     validationLevel: <string>,
     validationAction: <string>,
     indexOptionDefaults: <document>,
     viewOn: <string>,              // Added in MongoDB 3.4
     pipeline: <pipeline>,          // Added in MongoDB 3.4
     collation: <document>,         // Added in MongoDB 3.4
     writeConcern: <document>
   }
)

ex) 예
db.createCollection("articles", {
    capped: true,
    autoIndex: true,
    size: 6142800,
    max: 10000
  })
{ "ok" : 1 }

show collections : 컬렉션이 무엇이 있는지 보여주는 명령어

db."컬렉션 이름".remove(
   <query>,
   <justOne>
) : 컬렉션 삭제 명령어

ex) 예
db.bios.remove({}) : all 삭제
db.products.remove({ qty: { $gt: 20 } }) : $gt 해당하는 Documents 모두 삭제!!

 

 

 

 

 

Document( SQL 에서 row와 비슷한 개념!!)

명령어

>db."컬렉션명".insert(document) : document 생성
ex)
> db.user.insert(
	[ 
    	{"name": "yunis", "phone": "010-xxxx-xxxx"}, 
        {"name": "testName", "phone": "010-xxxx-xxxx"} 
    ]);

>db."컬렉션명".find([query, projection]) : document 조회
ex) 
>db.articles.find( { } , { “_id”: false, “title”: true, “content”: true } )

>db."컬렉션명".remove(criteria[, justOne]) : document 삭제

 

 

 

 

 

 

쿼리 연산자

쿼리 연산자부분은 아까전에도 사용했지만 이쪽은 양이 너무 많아서 공식문서로 링크를 올려두겠습니다.

docs.mongodb.com/manual/reference/operator/query/

 

Query and Projection Operators — MongoDB Manual

Note For details on specific operator, including syntax and examples, click on the specific operator to go to its reference page.

docs.mongodb.com