본문 바로가기
IT/MongoDB

MongoDB 연산자 정리

by Blog37 2024. 5. 10.
반응형

# 쿼리 연산자

비교

  • $eq :    값이 같음. (필드 == 값)
    ex) db.users.find({ age: { $eq: 2 } })
  • $ne :    값이 같지 않음. (필드 != 값)
    ex) db.users.find({ age: { $ne: 2 } })
  • $gt :    값이 필드 값보다 큼. (필드 > 값)
    ex) db.users.find({ age: { $gt: 2 } })
  • $gte :    값이 필드 값보다 크거나 같음. (필드 >= 값)
    ex) db.users.find({ age: { $gte: 2 } })
  • $lt :    값이 필드 값보다 작음. (필드 < 값)
    ex) db.users.find({ age: { $lt: 3 } })
  • $lte :    값이 필드 값보다 작거나 같음. (필드 <= 값)
    ex) db.users.find({ age: { $lte: 3 } })
  • $in :    배열 내에서 값이 일치함. (필드 == 값)
    ex) db.users.find({ age: { $in: [2, 3] } })
  • $nin :    배열 내에서 값이 일치하지 않음. (필드 != 값)
    ex) db.users.find({ age: { $nin: [2, 7] } })

논리

  • $and :    두 쿼리가 모두 다 일치하는 도큐먼트를 반환. (쿼리 && 쿼리)
    ex) db.users.find({ $and: [ {name: "흰둥이"}, {age: 3} ] })
  • $or :    쿼리 중 일치하는 도큐먼트를 반환. (쿼리 || 쿼리)
    ex) db.users.find({ $or: [ {name: "흰둥이"}, {age: 2} ] })
  • $nor :    두 쿼리가 모두 다 일치하지 않는 도큐먼트를 반환.
    ex) db.users.find({ $nor: [ {name: "초롱이"}, {age: 3} ] })
  • $not :    쿼리가 일치하지 않는 도큐먼트를 반환.

평가

  • $regex :    정규 표현식를 사용하여 도큐먼트 일치.
    ex) db.users.find({name: { $regex: /흰둥/, $options:"i"}})
  • $where :    JavaScript를 사용하여 도큐먼트 일치.
    ex) db.users.find({ $where: "this.name == '흰둥이' && this.age == 3"})
  • $elemMatch

 

# 업데이트 연산자

필드

  • $set :    필드 값을 변경 및 설정.
    ex) db.users.updateMany({ name: "흰둥이" }, { $set: { name : "초롱이" } })
  • $max :    기존 필드 값 보다 새로 변경되는 값이 클 경우에만 변경.
    ex) db.users.updateMany({ }, { $max: { age : 7 } })
  • $min :    기존 필드 값 보다 새로 변경되는 값이 작을 경우에만 변경.
    ex) db.users.updateMany({ }, { $min: { age : 3 } })
  • $inc :    필드 값을 증가.
    ex) db.users.updateMany({ }, { $inc: { age : 1 } })
  • $mul :    필드 값을 곱셈.
    ex) db.users.updateMany({ }, { $mul: { age : 3 } })
  • $rename :    필드 이름 변경.
    ex) db.users.updateMany({ }, { $rename: { age:"나이" } })
  • $unset :    필드 완전 삭제.
    ex) db.users.updateMany({ }, { $unset: { age: "" } })
  • $currentDate :    필드 값을 현재 날짜로 설정.

배열

  • $push :    배열에 요소 한 개 추가.
    ex) db.users.updateMany({ }, { $push: { arr: 1 } })
  • $each :    배열에 요소 여러 개 추가.
    ex) db.users.updateMany({ }, { $push: { arr: { $each:[2, 3] } } })
  • $addToSet :    배열에 중복된 값이 없을 경우에만 요소를 (한 개) 추가.
    ex) db.users.updateMany({ }, { $addToSet: { arr: 7 } })
  • $pop :    배열의 첫 번째 요소를 제거.
    ex) db.users.updateMany({ }, { $pop: { arr: -1 } })
  • $pop :    배열의 마지막 요소를 제거.
    ex) db.users.updateMany({ }, { $pop: { arr: 1 } })
  • $pull :    배열에서 쿼리와 일치하는 모든 요소를 제거.
    ex) db.users.updateMany({ }, { $pull: { arr: 7 } })

Tip

  • 배열 안에 오브젝트 요소를 수정 할 경우
    // 도큐먼트 예시: { _id: ObjectId(),
                                 name: "흰둥이"
                                 age: 3
                                 arr: [ {key_1:"value_1"}, {key_2:"value_2"} ]
                                 }
    // arr 배열 안에 오브젝트 key_1값을 수정.
    ex1) db.users.updateOne({ name:"흰둥이" },
                { $set: { "arr.$[el].key_1": "value_update" } },
                { arrayFilters: [{ "el.key_1": "value_1" }] }
            )
    // arr 배열의 [1]번째 요소의 오브젝트 key_2값을 수정.
    ex2) db.users.updateMany({ },
                { $set: { "arr.1.key_2": "value_update" } }
            )

https://blog37.tistory.com/entry/MongoDB-%EA%B8%B0%EB%B3%B8-%EB%AA%85%EB%A0%B9%EC%96%B4-%EC%A0%95%EB%A6%AC

 

MongoDB 기본 명령어 정리

# 계정 관리 명령어생성된 계정 목록 보기 :    show users계정 생성 하기 :    db.createUser() ex) db.createUser({ user: "root", pwd: "1111", roles: ["root"] })계정 삭제 하기 :    db.dropUser() ex) db.dropUser("root")계정

blog37.tistory.com

 

반응형

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

MongoDB 기본 명령어 정리  (0) 2024.05.10
window에서 MongoDB 설치 및 실행  (0) 2024.05.10