티스토리 뷰

Study/MongoDB

[MongoDB] CRUD

Hoon's Blog 2020. 2. 7. 18:05

1. MongoDB 설치 및 실행

이전글 참고 : https://hoonjo.tistory.com/21

 

[MongoDB] MongoDB 설치

1. MongoDB? 요약. # MongoDB는 대용량 데이터를 처리하기에 적합한 NoSQL DB이다. 더보기 NoSQL? No SQL, Not Only SQL, Non-Relational Operational Database SQL 등으로 불림. 기존의 관계형 데이터베이스(RDBM..

hoonjo.tistory.com

# MongoDB 실행

mongod --dbpath  C:\MongoDB\db

 


 

2. MongoDB Shell 접속

 

> mongo

PS C:\Users\line play> mongo
MongoDB shell version v4.2.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("생략") }
MongoDB server version: 4.2.3
Server has startup warnings:
2020-02-07T18:03:30.446+0900 I  CONTROL  [initandlisten]
2020-02-07T18:03:30.446+0900 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-02-07T18:03:30.447+0900 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2020-02-07T18:03:30.447+0900 I  CONTROL  [initandlisten]
2020-02-07T18:03:30.447+0900 I  CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2020-02-07T18:03:30.447+0900 I  CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server.
2020-02-07T18:03:30.447+0900 I  CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP
2020-02-07T18:03:30.447+0900 I  CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2020-02-07T18:03:30.447+0900 I  CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2020-02-07T18:03:30.448+0900 I  CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2020-02-07T18:03:30.449+0900 I  CONTROL  [initandlisten]
MongoDB Enterprise >

3. Create

 

(1) Database 생성

> use Database명

MongoDB Enterprise > use crud-test
switched to db crud-test
MongoDB Enterprise >

 

(2) 사용중인 Database 확인

> db

MongoDB Enterprise > db
crud-test

(3) 데이터베이스 리스트 확인

 

> show dbs

MongoDB Enterprise > show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

# 단, 아직 crud-test는 리스트에 출력되지 않음. 최소 1개 이상의 Document를 추가하여야 database 생성됨.

 

 

(4) insert 메소드를 사용하여 collection에 document를 insert 함. 

 

> db.books.insert({ title: "Example1", author: "Lee", price: 100 })

(db.컬렉션명.insert)

 

> show dbs

# insert 후 collection book이 생성되었는지 확인.

MongoDB Enterprise > db.books.insert({ title: "Example1", author: "Lee", price: 100 })
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > show dbs
admin      0.000GB
config     0.000GB
crud-test  0.000GB
local      0.000GB
MongoDB Enterprise >

 

(5) Collection List 확인

show collections 또는 db.getCollectionNames()를 통해 Collection List 확인

MongoDB Enterprise > show collections
books

MongoDB Enterprise > db.getCollectionNames()
[ "books" ]
MongoDB Enterprise >

4. Read

 

# 기본 구조

db.collection.find(query, projection)

 

find() 메소드를 사용하여 "books"이라는 collection의 document를 select하면 다음과 같다.

> db.books.find()

MongoDB Enterprise > db.books.find()
{ "_id" : ObjectId("5e3d2a8f59a340cad2455c10"), "title" : "Example1", "author" : "Lee", "price" : 100 }
MongoDB Enterprise >

상세 내용은 아래 링크 (https://poiemaweb.com/mongdb-basics-shell-crud)에서 확인


5. Update

 

# 기본 구조

db.<collection_name>.update(
  <query>,
  <update>,
  {
    upsert: <boolean>,
    multi: <boolean>,
    writeConcern: <document>
  }
)

 

예를 들어, 

 

Collection "books"에서 title이 Example1인 경우 author를 Lee에서 Jo로 업데이트를 할 경우,

 

MySQL로 포현하면 다음과 같다.

UPDATE books SET author = "Jo" WHERE title = "Example1"

 

이를 MongoDB에서는 다음과 같이 표현할 수 있다.

db.books.update( { title: { $eq: "Example1" } }, { $set: { author: "Jo" } }, { multi: true } )

 

# 확인

MongoDB Enterprise > db.books.find()
{ "_id" : ObjectId("5e3d2a8f59a340cad2455c10"), "title" : "Example1", "author" : "Lee", "price" : 100 }


MongoDB Enterprise > db.books.update( { title: { $eq: "Example1" } }, { $set: { author: "Jo" } }, { multi: true } )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })


MongoDB Enterprise > db.books.find()
{ "_id" : ObjectId("5e3d2a8f59a340cad2455c10"), "title" : "Example1", "author" : "Jo", "price" : 100 }
MongoDB Enterprise >

 


6. Delete

 

# 기본 구조

db.collection.remove(
  <query>,
  {
    justOne: <boolean>,
    writeConcern: <document>
  }
)

 

# title이 "Example2"인 document 삭제

MongoDB Enterprise > db.books.find()
{ "_id" : ObjectId("5e3d2a8f59a340cad2455c10"), "title" : "Example1", "author" : "Jo", "price" : 100 }
{ "_id" : ObjectId("5e3d323759a340cad2455c11"), "title" : "Example2", "author" : "Lee", "price" : 100 }


MongoDB Enterprise > db.books.remove( { title: { $eq: "Example2" } } )
WriteResult({ "nRemoved" : 1 })


MongoDB Enterprise > db.books.find()
{ "_id" : ObjectId("5e3d2a8f59a340cad2455c10"), "title" : "Example1", "author" : "Jo", "price" : 100 }
MongoDB Enterprise >

 

# "books" Collection의 모든 document를 삭제

MongoDB Enterprise > db.books.remove( { } )
WriteResult({ "nRemoved" : 1 })

MongoDB Enterprise > db.books.find()
MongoDB Enterprise >

 

# "books" Collection 삭제

MongoDB Enterprise > show collections
books


MongoDB Enterprise > db.books.drop()
true


MongoDB Enterprise > show collections
MongoDB Enterprise >

 

# "crud-test" Database 삭제

MongoDB Enterprise > use crud-test
switched to db crud-test


MongoDB Enterprise > db.dropDatabase();
{ "dropped" : "crud-test", "ok" : 1 }
MongoDB Enterprise >

 


Ref. CRUD - https://poiemaweb.com/mongdb-basics-shell-crud

 

MongoDB Shell - CRUD | PoiemaWeb

MongoDB Shell method를 사용한 CRUD의 기본

poiemaweb.com

 

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

[MongoDB] Mongoose module 설치  (0) 2020.02.11
[MongoDB] MongoDB Plugin 설치 (IntelliJ)  (0) 2020.02.07
[MongoDB] MongoDB 설치  (1) 2020.02.07
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함