ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [MongoDB] CRUD Operations
    공부/MongoDB 2022. 1. 9. 19:24

    Create

    • insertOne(data, options) 👉 하나의 document를 삽입함
    • insertMany(data, options) 👉 여러 document를 삽입함

    Read

    • find(filter, options) 👉 collection의 모든 document를 조회함
    • findOne(filter, options) 👉 filter를 만족하는 첫번째 document를 조회함

     

    Update

    • updateOne(filter, data, options) 👉 filter를 만족하는 첫번째 document를 수정함
    • updateMany(filter, data, options) 👉 filter를 만족하는 모든 document를 수정함
    • replaceOne(filter, data, options) 👉 filter를 만족하는 첫번째 document 전체를 교체함

    📢 updateOne: field 단위 수정

         replaceOne: document 단위 수정

     

    Delete

    • deleteOne(filter, options) 👉 filter를 만족하는 첫번째 document를 삭제함
    • deleteMany(filter, options) 👉 filter를 만족하는 모든 document를 삭제함

    deleteOne( )
    updateOne( )

    updateOne( ) 사용시 {$set: {key : value}}로 업데이트해줘야함

     

    updateMany( )

     

    deleteMany( )

     

    insertMany( )

    insertMany( [ document1, document2, ... ] )

    리스트에 저장된 document 데이터 순서대로 _id 값이 증가하는 것을 확인 👉 collection에 순차적으로 데이터가 저장됨

     

     

    find( ), findOne( )

    정수형 값 비교 👉 {key: {$value: val}}

    findOne( )은 pretty( ) 지원 ❌

     

    updateOne( )

     

    updateOne( )

     

    updateOne( ) 으로 기존의 document 값이 변경되면 modifiedCount : 1, 변경되지 않으면 modifiedCount : 0

     

    update( )

     

    update( ) 사용 시 {$set: {key : value}}가 아닌 {key : value}로 document 수정 가능

    updateOne( ), updateMany( )와 달리 아예 document 전체 값을 바꿈

     

    replaceOne( )

     

    update( ) 보다 replaceOne( )을 쓰는 것이 더 안전함

     

     

    '공부 > MongoDB' 카테고리의 다른 글

    [MongoDB] projection  (0) 2022.01.09
    [MongoDB] find( )  (0) 2022.01.09
    [MongoDB] document의 _id를 지정해서 데이터 삽입하기  (0) 2022.01.03
    [MongoDB] MongoDB 서버 실행  (0) 2022.01.02
    [MongoDB] 데이터 삽입 및 조회  (0) 2022.01.02
Designed by Tistory.