데이터베이스/MongoDB
MongoDB 소개 및 설치, 간단한 사용법
괴발자-K
2022. 6. 8. 11:39
NoSQL? MongoDB?
아래 링크를 꼭 참고 하길 바랍니다.
https://kciter.so/posts/about-mongodb
MongoDB 이해하기
사내에서 MongoDB를 잘 쓰기위한 스터디를 하게되어 이번 기회에 관련 자료를 정리하기로 했다. MongoDB가 왜 필요한지, 더 잘사용하기 위해서 무엇이 필요한지를 중심으로 처음 MongoDB를 사용할 때
kciter.so
1. WINDOW에 MongoDB 설치
https://www.mongodb.com/try/download/enterprise
MongoDB Enterprise Server Download
Download MongoDB Enterprise Server, which provides advanced security and performance options for the most demanding apps. Use for free for development.
www.mongodb.com
- Package를 MSI으로 다운로드
- 설치 경로가 디폴트로 C:\Program Files\MongoDB\Server\5.0 설정 됌
2. 환경변수 설정
- 시스템 속성 < 환경 변수 < Path < 편집 < 새로만들기 < C:\Program Files\MongoDB\Server\5.0\bin 추가
3. MongDB 실행
CMD 실행
- 서버실행
C:\Users\~~ > mongd
- DB 접속
C:\Users\~~ > mongo
MongoDB shell version v5.0.9 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("e98deb2b-1f4e-4b99-8ea4-85a9c9b4961b") } MongoDB server version: 5.0.9
================
Warning: the "mongo" shell has been superseded by "mongosh", which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in an upcoming release. For installation instructions, see https://docs.mongodb.com/mongodb-shell/install/
================
Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see https://docs.mongodb.com/ Questions? Try the MongoDB Developer Community Forums https://community.mongodb.com --- The server generated these startup warnings when booting: 2022-06-08T10:27:30.374+09:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted ---
4. 간단한 조작법
- 데이터베이스 생성
MongoDB Enterprise > use test_db
switched to db test_db
- 현재 실행 중인 DB 확인
MongoDB Enterprise > db
test_db
- Document 생성 및 생성한 db 확인
- Document를 최소 1개 이상 생성을 해야 확인이 가능
MongoDB Enterprise > db.book.insert({"name":"Yamea MongDB", "author":"cheesu"});
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
test_db 0.000GB
- 데이터베이스 제거
MongoDB Enterprise > db.dropDatabase();
{ "ok" : 1 }
- Collection 생성
MongoDB Enterprise > db.createCollection("book")
{ "ok" : 1 }
- Collection + 옵션 생성
MongoDB Enterprise > db.createCollection("articles", {capped:true, size:6142800. max:10000})
{ "ok" : 1 }
※MongoDB Enterprise > db.insert({"name":"cheesu"}) : Document 생성 시에도 Collection이 자동으로 생성
- 생성한 Collection 확인
MongoDB Enterprise > show collections
articles
book
people -> Document 생성 방법으로 collection 생성
- Conllection 삭제
MongoDB Enterprise > db.people.drop()
true
MongoDB Enterprise > show collections
articles
book
- Document 추가
- 하나의 document 추가
MongoDB Enterprise > db.book.insert({"name" : "Book1", "author" :"Cheesu"})
BulkWriteResult({ "writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 1,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ] })
- 여러개의 document 추가
MongoDB Enterprise > db.book.insert([{"name":"Book1","author":"Cheesu"}, {"name":"Book2", "author":"Yamea"}])
BulkWriteResult({ "writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 2,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ] })
- Collection 다큐먼트 확인
MongoDB Enterprise > db.book.find()
{ "_id" : ObjectId("62a00073a4d4dd691cb6a22b"), "name" : "Book1", "author" : "Cheesu" } { "_id" : ObjectId("62a000a3a4d4dd691cb6a22c"), "name" : "Book1", "author" : "Cheesu" } { "_id" : ObjectId("62a000a3a4d4dd691cb6a22d"), "name" : "Book2", "author" : "Yamea" }
※ Book1은 같은 데이터 이지만 id 값이 다름
- Document 제거
- db.컬렉션이름.remove(criteria, justOne)
- critetia : 삭제할 데이터의 기준값. 이 값이 {} 이면 컬렉션의 모든 데이터를 제거
- justOne : 선택적 매개변수이며 이 값이 true면 1개의 다큐먼트만 제거 합니다. 생략하면 기본값은 false이며 criteria에 해당되는 모든 다큐먼트들을 제거 한다
MongoDB Enterprise > db.book.remove({"name":"Book1"})
WriteResult({ "nRemoved" : 2 })
출처: https://yamea-guide.tistory.com/entry/MongoDB-기본-명령어 [기타치는 개발자의 야매 가이드:티스토리]