기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.
$limit
Amazon DocumentDB의 $limit 연산자는 쿼리에서 반환되는 문서 수를 제한하는 데 사용됩니다. MongoDB $limit 연산자와 비슷하지만 Amazon DocumentDB와 함께 사용할 때 몇 가지 구체적인 고려 사항이 있습니다.
Amazon DocumentDB에서 $limit 연산자는 일치하는 전체 문서의 하위 집합을 검색하려는 페이지 매김에 유용합니다. 이를 통해 각 응답에서 반환되는 문서 수를 제어하여 성능을 개선하고 네트워크를 통해 전송되는 데이터의 양을 줄일 수 있습니다.
파라미터
예제(MongoDB 쉘)
다음 예제에서는 $limit 연산자를 사용하여 지정된 필터와 일치하는 최대 하나의 문서를 반환하는 방법을 보여줍니다.
샘플 문서 생성
db.test.insertMany([
{ "_id": 1, "star_rating": 4, "comments": "apple is red" },
{ "_id": 2, "star_rating": 5, "comments": "comfortable couch" },
{ "_id": 3, "star_rating": 3, "comments": "apples, oranges - healthy fruit" },
{ "_id": 4, "star_rating": 5, "comments": "this is a great couch" },
{ "_id": 5, "star_rating": 5, "comments": "interesting couch" }
]);
쿼리 예제
db.test.createIndex({ comments: "text" });
db.test.find({
$and: [
{ star_rating: 5 },
{ $text: { $search: "couch" } }
]
}).limit(1);
출력
[ { _id: 2, star_rating: 5, comments: 'comfortable couch' } ]
코드 예제
$limit 명령을 사용하기 위한 코드 예제를 보려면 사용하려는 언어의 탭을 선택합니다.
- Node.js
-
const { MongoClient } = require('mongodb');
async function limitExample() {
const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false');
try {
await client.connect();
const db = client.db('test');
const collection = db.collection('test');
await collection.createIndex({ comments: 'text' });
const query = {
$and: [
{ star_rating: 5 },
{ $text: { $search: "couch" } }
]
};
const result = await collection.find(query).limit(1).toArray();
console.log(result);
} catch (err) {
console.error("Error:", err);
} finally {
await client.close();
}
}
limitExample();
- Python
-
from pymongo import MongoClient
def limit_example():
try:
client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false')
db = client['test']
collection = db['test']
collection.create_index([('comments', 'text')])
query = {
'$and': [
{'star_rating': 5},
{'$text': {'$search': 'couch'}}
]
}
result = collection.find(query).limit(1)
for doc in result:
print(doc)
except Exception as e:
print(f"An error occurred: {e}")
finally:
client.close()
limit_example()