AI 에이전트의 경우: 문서 인덱스는 https://www.mongodb.com/ko-kr/docs/llms.txt에서 사용할 수 있으며, 모든 페이지의 마크다운 버전은 어떤 URL 경로에 .md를 추가하여 사용할 수 있습니다.
Docs Menu

문서 수 계산

Collection 인스턴스에서 다음 메서드 중 하나를 호출하여 컬렉션의 문서 수를 계산할 수 있습니다.

각 메서드는 개수를 u64 인스턴스로 반환합니다.

참고

count_documents() 메서드에 필터를 전달하지 않으면 MongoDB는 collection의 총 문서 수를 계산합니다.

이 예에서는 sample_restaurants 데이터베이스의 restaurants collection에 있는 문서 수를 계산합니다.

다음 코드에서는 먼저 estimated_document_count() 메서드를 사용하여 collection의 총 문서 수를 계산합니다. 그런 다음 count_documents() 메서드를 사용하여 쿼리 필터와 일치하는 문서 수를 계산합니다. 이 필터는 name 필드 값에 문자열 "Sunset" 가 포함된 문서와 일치합니다.

Asynchronous 또는 Synchronous 탭을 선택하여 각 런타임에 해당하는 코드를 확인합니다.

use std::env;
use mongodb::{ bson::doc, Client, Collection };
use bson::Document;
#[tokio::main]
async fn main() -> mongodb::error::Result<()> {
let uri = "<connection string>";
let client = Client::with_uri_str(uri).await?;
let my_coll: Collection<Document> = client
.database("sample_restaurants")
.collection("restaurants");
let ct = my_coll.estimated_document_count(None).await?;
println!("Number of documents: {}", ct);
let ct = my_coll.count_documents(doc! { "name": doc! { "$regex": "Sunset" } }, None).await?;
println!("Number of matching documents: {}", ct);
Ok(())
}
// Your values might differ
Number of documents: 25216
Number of matching documents: 10
use std::env;
use mongodb::{
bson::{ Document, doc },
sync::{ Client, Collection }
};
fn main() -> mongodb::error::Result<()> {
let uri = "<connection string>";
let client = Client::with_uri_str(uri)?;
let my_coll: Collection<Document> = client
.database("sample_restaurants")
.collection("restaurants");
let ct = my_coll.estimated_document_count(None)?;
println!("Number of documents: {}", ct);
let ct = my_coll.count_documents(doc! { "name": doc! { "$regex": "Sunset" } }, None)?;
println!("Number of matching documents: {}", ct);
Ok(())
}
// Your values might differ
Number of documents: 25216
Number of matching documents: 10