Database/MongoDB

[MongoDB] Find Documents

Any Developer 2021. 11. 11. 22:14

[서론]

MongoDB 에서 Find Document Query 문에 대해 정리한다.

 

 

Find Documents — MongoDB Realm

 

Find Documents — MongoDB Realm

Docs Home → MongoDB RealmThe code snippets on this page demonstrate how to read documents that are stored in a MongoDB collection. Read operations use query filters to specify which documents to return from the database. This page also covers several dif

docs.mongodb.com

 

 

[본론]

 

1. 일치 찾기

 

1.1. 문자열 찾기

Query Statement

{"item":"journal"}

-> item 이라는 키에 journal 문자열값이 들어가 있는 documents 모두 검색

 

1.2. 숫자 찾기

Query Statement

{"qty":50}

-> qty 키에 값 50인 docuements 모두 검색

 

1.3. 불리언값 찾기

Query Statement

{"boolval":true}

-> boolval 키에 값 true documents 모두 검색

 

 

2. 복합 검색 (일치)

 

2.1. AND 일치 검색

{"키": 값포맷 , "키2":값포맷, ....} 형식

 

{"qty" : 50, "boolval2" : false }

-> qty 키에 값 50 and boolval2 키에 값 false 인 documents 모두 검색

 

3. $in 연산자

{ "qty" : { $in : [ 5, 15 ]  } }

-> qty 필드에 값 5 혹은 15 인 document 모두 검색

 

{ "qty" : { $in : [ 5, 15 ]  } , "boolval" : true }

-> -> qty 필드에 값 5 혹은 15 이고(and) boolval 필드에 값 true 인 document 모두 검색

 

$in — MongoDB Manual

 

$in — MongoDB Manual

Docs Home → MongoDB Manual$inThe $in operator selects the documents where the value of a field equals any value in the specified array. To specify an $in expression, use the following prototype:For comparison of different BSON type values, see the specif

docs.mongodb.com

 

 

4. $eq 연산자

{"qty" : 50}

{"qty" : { $eq : 50 } }

-> 두개가 같다고 한다...

 

$eq — MongoDB Manual

 

$eq — MongoDB Manual

Docs Home → MongoDB Manual$eqSpecifies equality condition. The $eq operator matches documents where the value of a field equals the specified value.{ : { $eq: } }Specifying the $eq operator is equivalent to using the form { field: } except when the is a

docs.mongodb.com

 

 

5. $ne 연산자

{ "boolval" : { $ne : false } }

-> boolval 필드에 값 false가 아닌 document 모두 검색

 

6. $gt , $gte , $lt , $lte 연산자

$gt : greater then (보다 크다)

$gte : greater then or equal (보다 크거나 같다)

$lt : less then (보다 작다)

$lte : less then or eque (보다 작거나 같다)

 

{ "qty" : { $gt : 50} }

-> qty 가 50 초과인 문서 검색

 

{ "qty" : { $gt : 50 , $lte : 60} }

-> qty 가 50 초과 그리고 60 이하인 문서 검색

 

{ "qty" : { $gt : 50 , $lte : 60, $ne : 55} }

-> qty 가 50 초과 그리고 60 이하 그리고 55가 아닌 인 문서 검색

 

7. $nin 연산자

{ "qty" : { $nin : [ 5, 15, 70 ]  } }

-> qty 의 값이 5, 15, 70 이 아닌 문서 검색

 

 

 

[결론]

위 내용으로 충분히 기능적으로는 테스트가 되었다.  그리고 조금만 응용하면 충분히 어플리케이션 기능도 구현이 가능할 것 같다.. 하지만 사용자 인터페이스와 연동하여 자유도를 높이기 위한 처리 작업은 만만치 않을 듯 하다.  만약 딱 원하는 기능만 버튼 한두개로 지정하면 수월할 듯 한데 여러가지 자유도를 주기 위한 일련의 작업들은 더 많은 고민을 해봐야 할 듯 하다.

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

[MongoDB] JSON & BSON 의 차이  (0) 2021.11.12
[MongoDB] Insert Documents  (0) 2021.11.11
[MongoDB] 외부 접속 하용하기 (윈도우)  (0) 2021.11.11
[MongoDB] connection String  (0) 2021.11.11
[MongoDB] User 생성하기  (0) 2021.11.11