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

여러 문서 찾기

Collection 인스턴스 에서 find() 메서드를 호출하여 컬렉션 에 있는 여러 문서를 쿼리 할 수 있습니다.

collection에서 필터와 일치하는 문서를 반환하려면 쿼리 필터를 find() 메서드에 전달합니다. 필터를 포함하지 않으면 MongoDB는 collection의 모든 문서를 반환합니다.

문서 검색에 대해 자세히 알아보려면 데이터 검색 가이드를 참조하고 쿼리 필터 만들기에 대해 자세히 알아보려면 쿼리 지정 가이드를 참조하세요.

find() 메서드는 개별 문서를 조회 위해 반복할 수 있는 커서 유형을 반환합니다. 커서 사용에 대해 자세히 학습 커서를 사용하여 데이터 액세스 가이드 참조하세요.

이 예에서는 sample_restaurants 데이터베이스의 restaurants collection에서 쿼리 필터와 일치하는 문서를 조회합니다. 이 예제에서는 조회된 문서의 데이터로 Restaurant 구조체의 인스턴스를 채웁니다.

다음 코드는 cuisine 필드의 값이 "French" 인 문서와 일치하는 쿼리 필터를 사용합니다.

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

use mongodb::{
bson::doc,
Client,
Collection
};
use futures::TryStreamExt;
use serde::{ Deserialize, Serialize };
#[derive(Serialize, Deserialize, Debug)]
struct Restaurant {
name: String,
cuisine: String,
}
#[tokio::main]
async fn main() -> mongodb::error::Result<()> {
let uri = "<connection string>";
let client = Client::with_uri_str(uri).await?;
let my_coll: Collection<Restaurant> = client
.database("sample_restaurants")
.collection("restaurants");
let mut cursor = my_coll.find(
doc! { "cuisine": "French" },
None
).await?;
while let Some(doc) = cursor.try_next().await? {
println!("{:?}", doc);
}
Ok(())
}
// Results truncated
...
Restaurant { name: "Cafe Un Deux Trois", cuisine: "French" }
Restaurant { name: "Calliope", cuisine: "French" }
...
use mongodb::{
bson::doc,
sync::{Client, Collection}
};
use serde::{ Deserialize, Serialize };
#[derive(Serialize, Deserialize, Debug)]
struct Restaurant {
name: String,
cuisine: String,
}
fn main() -> mongodb::error::Result<()> {
let uri = "<connection string>";
let client = Client::with_uri_str(uri)?;
let my_coll: Collection<Restaurant> = client
.database("sample_restaurants")
.collection("restaurants");
let mut cursor = my_coll.find(
doc! { "cuisine": "French" },
None
)?;
for result in cursor {
println!("{:?}", result?);
}
Ok(())
}
// Results truncated
...
Restaurant { name: "Cafe Un Deux Trois", cuisine: "French" }
Restaurant { name: "Calliope", cuisine: "French" }
...