본문 바로가기
IT

mongo crud 연산 명령어

by 허브큐 2021. 9. 17.
  • database 생성 : 데이터베이스를 생성하는 별도의 방법은 없으며 컬렉션에 쓰기를 하면 자동으로 생성됨
  • 데이터 등록 - insert
    • db.{collection-name}.insert({json})
    • db.users.insert({username: "smith"})
  • 데이터 조회 - select
    • db.{collection-name}.find()
    • db.users.find()
    • db.users.find().pretty()
    • db.users.find({username: "smith"})
  • 데이터 업데이트 - update
    • db.{collection-name}.update()
    • 연산자 업데이트 : db.users.update({username: "smith"}, {$set: {country: "Canada"}})
      • 해당 필드만 업데이트됨
    • 대체 업데이트 : db.users.update({username: "smith"}, { {country: "Canada"}})
      • document가 대체됨
    • update 연산자 정리
      • $set
      • $push : 
      • $addToSet : 중복되지 않음
  • 데이터 삭제 - remove
    • db.users.remove({}) : 모든 도큐먼트 삭제
    • db.users.remove(){조건} : 조건과 일치하는 도큐먼트 삭제
  • 컬렉션 삭제 - drop
    • db.users.drop() : 도큐먼트, 인덱스 삭제 
  •  count
    • db.users.count()
  • 인덱스 & explain()
    • db.users.find().explain()
    • db.users.find().explain("executionStats")
    • db.users.getIndexes()
  • 데이터베이스 정보 얻기
    • show dbs - database list
    • show collections - collection list
    • db.stats() - db 상태정보
    • db.users.stats() - collection 상태정보
  •  

'IT' 카테고리의 다른 글

mongodb collection  (0) 2021.09.18
Installing mongodb with Homebrew  (0) 2021.09.10
MapStruct 설정  (0) 2021.06.22

댓글