정의
cursor.min()중요
Mongo쉬 방법
This page documents a
mongoshmethod. This is not the documentation for a language-specific driver, such as Node.js.MongoDB API 드라이버의 경우 언어별 MongoDB 드라이버 설명서를 참조하세요.
Specifies the inclusive lower bound for a specific index in order to constrain the results of
find().min()provides a way to specify lower bounds on compound key indexes.The
min()method has the following parameter:Parameter유형설명indexBounds문서
인덱스 키의 포괄적 하한값입니다.
indexBounds매개변수의 프로토타입 형식은 다음과 같습니다.{ field1: <min value>, field2: <min value2>, fieldN:<min valueN> }
참고
인덱스 사용하기
호환성
이 메서드는 다음 환경에서 호스팅되는 배포에서 사용할 수 있습니다.
- MongoDB Atlas: 클라우드에서의 MongoDB 배포를 위한 완전 관리형 서비스
참고
이 명령은 모든 MongoDB Atlas 클러스터에서 지원됩니다. 모든 명령에 대한 Atlas 지원에 관해 자세히 알아보려면 지원되지 않는 명령을 참조하세요.
MongoDB Enterprise: MongoDB의 구독 기반 자체 관리 버전
MongoDB Community: MongoDB의 소스 사용 가능 무료 자체 관리 버전
동작
인덱스 선택과의 상호 작용
Because min() requires an index on a field, and forces the query to use this index, you may prefer the $gte operator for the query if possible. Consider the following example:
db.products.find( { _id: { $in: [ 6, 7 ] } } ).min( { price: Decimal128("1.39") } ).hint( { price: 1 } )
이 쿼리는 _id 의 인덱스가 더 나을 수 있더라도 price 필드의 인덱스를 사용합니다.
인덱스 바운드
If you use min() with max() to specify a range:
min() 미포함: max()
The min() and max() methods indicate that the system should avoid normal query planning. They construct an index scan where the index bounds are explicitly specified by the values given in min() and max().
경고
두 경계 중 하나를 지정하지 않으면 쿼리 계획은 한 쪽이 제한되지 않은 인덱스 스캔이 됩니다. 이렇게 하면 연산자를 모두 포함하지 않는 쿼리 또는 두 연산자를 모두 사용하여 인덱스 스캔을 보다 엄격하게 제한하는 쿼리에 비해 성능이 저하될 수 있습니다.
예시
Unless the find() query is an equality condition on the _id field { _id: <value> }, you must explicitly specify the index with the hint() method to run min().
아래 예시에서는 다음 문서를 포함하는 products 라는 이름의 샘플 컬렉션 을 만듭니다.
db.products.insertMany([ { "_id" : 1, "item" : "apple", "type" : "honey crisp", "price" : Decimal128("1.99") }, { "_id" : 2, "item" : "apple", "type" : "fuji", "price" : Decimal128("1.99") }, { "_id" : 3, "item" : "apple", "type" : "jonagold", "price" : Decimal128("1.29") }, { "_id" : 4, "item" : "apple", "type" : "jonathan", "price" : Decimal128("1.29") }, { "_id" : 5, "item" : "apple", "type" : "mcintosh", "price" : Decimal128("1.29") }, { "_id" : 6, "item" : "apple", "type" : "cortland", "price" : Decimal128("1.29") }, { "_id" : 7, "item" : "orange", "type" : "cara cara", "price" : Decimal128("2.99") }, { "_id" : 9, "item" : "orange", "type" : "satsuma", "price" : Decimal128("1.99") }, { "_id" : 8, "item" : "orange", "type" : "valencia", "price" : Decimal128("0.99") }, { "_id" : 10, "item" : "orange", "type" : "navel", "price" : Decimal128("1.39") } ])
컬렉션에 다음 인덱스를 만듭니다.
db.products.createIndexes( [ { "item" : 1, "type" : 1 }, { "item" : 1, "type" : -1 }, { "price" : 1 } ] )
Using the ordering of the
{ item: 1, type: 1 }index,min()limits the query to the documents that are at or above the index key bound ofitemequal toappleandtypeequal tojonagold, as in the following:db.products.find().min( { item: 'apple', type: 'jonagold' } ).hint( { item: 1, type: 1 } ) 이 쿼리는 다음 문서를 반환합니다.
{ "_id" : 3, "item" : "apple", "type" : "jonagold", "price" : Decimal128("1.29") } { "_id" : 4, "item" : "apple", "type" : "jonathan", "price" : Decimal128("1.29") } { "_id" : 5, "item" : "apple", "type" : "mcintosh", "price" : Decimal128("1.29") } { "_id" : 7, "item" : "orange", "type" : "cara cara", "price" : Decimal128("2.99") } { "_id" : 10, "item" : "orange", "type" : "navel", "price" : Decimal128("1.39") } { "_id" : 9, "item" : "orange", "type" : "satsuma", "price" : Decimal128("1.99") } { "_id" : 8, "item" : "orange", "type" : "valencia", "price" : Decimal128("0.99") } Using the ordering of the index
{ price: 1 },min()limits the query to the documents that are at or above the index key bound ofpriceequal to1.39andmax()limits the query to the documents that are below the index key bound ofpriceequal to1.99:db.products.find().min( { price: Decimal128("1.39") } ).max( { price: Decimal128("1.99") } ).hint( { price: 1 } ) 이 쿼리는 다음 문서를 반환합니다.
{ "_id" : 10, "item" : "orange", "type" : "navel", "price" : Decimal128("1.39") }