Etc

[MongoDB] 조건으로 조회하기

soyoung Park 2023. 5. 15. 23:40

MongoDB에서 여러가지 조건으로 조회하는 방법들을 정리하였다.

 

1. 존재 여부로 조회하기

db.getCollection('collection_nm').find({"item":"value"})

item에 value가 존재할 때 조회한다.

 

2. Key 값으로 조회하기

db.getCollection('collection_nm').find({"item.key":{"$exists":true}})

item, 확인할 key 값(key)이 존재할 때 조회한다.

 

3. 특정 값 이상으로 조회하기

db.getCollection('collection_nm').find({"item" : {"$gte":value}})

item이 value 이상일 때 조회한다.

 

4. 특정 값 이하로 조회하기

db.getCollection('collection_nm').find({"item" : {"$lte":value}})

item이 value 이하일 때 조회한다.

 

5. 특정 값 포함으로 조회하기

db.getCollection('collection_nm').find({"item" : {"$regex":"value"}})