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

빠른 참조 구문 예시

이 페이지에서는 Rust 드라이버를 사용하여 몇 가지 일반적인 MongoDB 작업을 수행하는 예제를 확인할 수 있습니다. 다음 표의 각 행에는 작업에 대한 설명, 작업을 실행하기 위한 드라이버 구문, 관련 참고 및 API 문서에 대한 링크가 포함되어 있습니다.

Rust 드라이버는 비동기 애플리케이션을 실행하기 위한 비동기 런타임을 제공합니다. 또한 드라이버는 블로킹 동기 런타임을 지원합니다. 다음 표에 나열된 각 MongoDB 작업에 대해 비동기 및 동기 API를 모두 사용하는 예제를 볼 수 있습니다.

비동기 및 동기 런타임에 대해 자세히 알아보려면 비동기 및 동기 API 가이드를 참조하세요.

명령
구문

비동기 런타임

let result = collection.find_one(doc! { "title": "Peter Pan" }).await?;

런타임 동기화

let result = collection.find_one(doc! { "title": "Peter Pan" }).run()?;

여러 문서 찾기

API 문서
문서 찾기

비동기 런타임

let filter = doc! { "year": 1925 };
let mut cursor = collection.find(filter).await?;

런타임 동기화

let filter = doc! { "year": 1925 };
let mut cursor = collection.find(filter).run()?;

비동기 런타임

let doc = doc! {
"title": "Mistress America", "type": "movie"
};
let result = collection.insert_one(doc).await?;

런타임 동기화

let doc = doc! {
"title": "Mistress America", "type": "movie"
};
let result = collection.insert_one(doc).run()?;

여러 문서를 삽입합니다.

API 문서
문서 삽입 가이드

비동기 런타임

let docs = vec![
doc! { "title": "Friends With Money", "runtime": 88 },
doc! { "title": "Please Give", "runtime": 90 },
doc! { "title": "You Hurt My Feelings", "runtime": 93 },
];
let result = collection.insert_many(docs).await?;

런타임 동기화

let docs = vec![
doc! { "title": "Friends With Money", "runtime": 88 },
doc! { "title": "Please Give", "runtime": 90 },
doc! { "title": "You Hurt My Feelings", "runtime": 93 },
];
let result = collection.insert_many(docs).run()?;

비동기 런타임

let filter = doc! { "title": "Burn After Reading"};
let update = doc! {
"$set": doc!{ "num_mflix_comments": 1 }
};
let result = collection.update_one(filter, update).await?;

런타임 동기화

let filter = doc! { "title": "Burn After Reading"};
let update = doc! {
"$set": doc!{ "num_mflix_comments": 1 }
};
let result = collection.update_one(filter, update).run()?;

여러 문서 업데이트하기

API 문서
문서 업데이트 가이드

비동기 런타임

let filter = doc! { "rated": "PASSED"};
let update = doc! {
"$set": doc!{ "rated": "Not Rated" }
};
let result = collection.update_many(filter, update).await?;

런타임 동기화

let filter = doc! { "rated": "PASSED"};
let update = doc! {
"$set": doc!{ "rated": "Not Rated" }
};
let result = collection.update_many(filter, update).run()?;

비동기 런타임

let filter = doc! { "title": "è Nous la Libertè" };
let replacement = doc! {
"title": "À nous la liberté",
"type": "movie",
"directors": vec! [ "René Clair" ]
};
let result = collection.replace_one(filter, replacement).await?;

런타임 동기화

let filter = doc! { "title": "è Nous la Libertè" };
let replacement = doc! {
"title": "À nous la liberté",
"type": "movie",
"directors": vec! [ "René Clair" ]
};
let result = collection.replace_one(filter, replacement).run()?;

비동기 런타임

let filter = doc! { "title": "Search and Destroy" };
let result = collection.delete_one(filter).await?;

런타임 동기화

let filter = doc! { "title": "Search and Destroy" };
let result = collection.delete_one(filter).run()?;

비동기 런타임

let filter = doc! {
"year": doc! { "$lt": 1920 }
};
let result = collection.delete_many(filter).await?;

런타임 동기화

let filter = doc! {
"year": doc! { "$lt": 1920 }
};
let result = collection.delete_many(filter).run()?;

커서에서 반복적으로 데이터에 액세스

API 문서
커서 가이드 에서 데이터 액세스

비동기 런타임

let mut cursor = collection
.find(doc! { "$and": vec!
[
doc! { "metacritic": doc! { "$gt": 90 } },
doc! { "directors": vec! [ "Martin Scorsese" ] }
] })
.await?;
while let Some(result) = cursor.try_next().await? {
println!("{}", result);
}

런타임 동기화

let cursor = collection
.find(doc! { "$and": vec!
[
doc! { "metacritic": doc! { "$gt": 90 } },
doc! { "directors": vec! [ "Martin Scorsese" ] }
] })
.run()?;
for result in cursor {
println!("{}", result?);
}

커서에서 배열로 데이터에 액세스

API 문서
커서 가이드 에서 데이터 액세스

비동기 런타임

let cursor = collection.find(doc! { "title": "Secrets & Lies" }).await?;
let results: Vec<Document> = cursor.try_collect().await?;

런타임 동기화

let cursor = collection.find(doc! { "title": "Secrets & Lies" }).run()?;
let results: Vec<Result<Document>> = cursor.collect();

비동기 런타임

let filter = doc! {
"languages": vec! [ "Mandarin" ]
};
let result = collection.count_documents(filter).await?;

런타임 동기화

let filter = doc! {
"languages": vec! [ "Mandarin" ]
};
let result = collection.count_documents(filter).run()?;

필드의 고유 값 나열

API 문서
고유 필드 값 가이드

비동기 런타임

let field_name = "title";
let filter = doc! {
"directors": vec! [ "Sean Baker" ]
};
let results = collection.distinct(field_name, filter).await?;

런타임 동기화

let field_name = "title";
let filter = doc! {
"directors": vec! [ "Sean Baker" ]
};
let results = collection.distinct(field_name, filter).run()?;

조회되는 문서 수 제한

API 문서
문서 가이드 지정

비동기 런타임

let filter = doc! { "awards.wins": 25};
let mut cursor = collection.find(filter).limit(5).await?;

런타임 동기화

let filter = doc! { "awards.wins": 25};
let mut cursor = collection.find(filter).limit(5).run()?;

조회된 문서 건너뛰기

API 문서
문서 가이드 지정

비동기 런타임

let filter = doc! { "runtime": 100 };
let mut cursor = collection.find(filter).skip(1).await?;

런타임 동기화

let filter = doc! { "runtime": 100 };
let mut cursor = collection.find(filter).skip(1).run()?;

조회 시 문서 정렬

API 문서
문서 가이드 지정

비동기 런타임

let filter = doc! {
"directors": vec! [ "Nicole Holofcener" ]
};
let mut cursor = collection
.find(filter)
.sort(doc! { "imdb.rating": 1 })
.await?;

런타임 동기화

let filter = doc! {
"directors": vec! [ "Nicole Holofcener" ]
};
let mut cursor = collection
.find(filter)
.sort(doc! { "imdb.rating": 1 })
.run()?;

조회 시 문서 필드 프로젝트

API 문서
필드 가이드 지정

비동기 런타임

let filter = doc! { "year": 2015 };
let mut cursor = collection
.find(filter)
.projection(doc! { "title": 1, "metacritic": 1, "_id": 0 })
.await?;

런타임 동기화

let filter = doc! { "year": 2015 };
let mut cursor = collection
.find(filter)
.projection(doc! { "title": 1, "metacritic": 1, "_id": 0 })
.run()?;

비동기 런타임

let index: IndexModel = IndexModel::builder().keys(doc! { "title": 1 }).build();
let result = collection.create_index(index).await?;

런타임 동기화

let index: IndexModel = IndexModel::builder().keys(doc! { "title": 1 }).build();
let result = collection.create_index(index).run()?;