문서 메뉴
문서 홈
/ / /
Node.js 드라이버
/ /

문서 삭제

collection.deleteOne()을 사용해 컬렉션에서 단일 문서를 삭제할 수 있습니다. deleteOne() 메서드는 사용자가 제공한 쿼리 문서를 사용하여 collection에서 쿼리와 일치하는 문서의 하위 집합을 일치시킵니다. 쿼리 문서를 제공하지 않거나 빈 문서를 제공하는 경우, MongoDB는 컬렉션의 모든 문서를 일치시키고 첫 번째 일치 문서를 삭제합니다.

메서드의 두 번째 매개변수로 전달된 객체를 사용하여 더 많은 쿼리 옵션을 지정할 수 options deleteOne 있습니다. 이 메서드에 대한 자세한 내용은 deleteOne() API 문서를 참조하세요.

참고

삭제 후 애플리케이션에 삭제된 문서가 필요한 경우 collection.findOneAndDelete()를 사용하는 것이 좋습니다. 해당 메서드는 deleteOne()과 유사한 인터페이스를 가지고 있지만 삭제된 문서도 반환합니다.

다음 스니펫은 movies 컬렉션에서 단일 문서를 삭제합니다. 이는 쿼리 문서를 사용해 title 값이 '애니 홀'인 동영상을 일치시키도록 쿼리를 구성합니다.

참고

이 예제를 사용하여 MongoDB 인스턴스에 연결하고 샘플 데이터가 포함된 데이터베이스와 상호 작용할 수 있습니다. MongoDB 인스턴스에 연결하고 샘플 데이터 세트를 로드하는 방법에 대해 자세히 알아보려면 사용 예제 가이드를 참조하세요.

1// Delete a document
2
3import { MongoClient } from "mongodb";
4
5// Replace the uri string with your MongoDB deployment's connection string
6const uri = "<connection string uri>";
7
8const client = new MongoClient(uri);
9
10async function run() {
11 try {
12 const database = client.db("sample_mflix");
13 const movies = database.collection("movies");
14
15 /* Delete the first document in the "movies" collection that matches
16 the specified query document */
17 const query = { title: "Annie Hall" };
18 const result = await movies.deleteOne(query);
19
20 /* Print a message that indicates whether the operation deleted a
21 document */
22 if (result.deletedCount === 1) {
23 console.log("Successfully deleted one document.");
24 } else {
25 console.log("No documents matched the query. Deleted 0 documents.");
26 }
27 } finally {
28 // Close the connection after the operation completes
29 await client.close();
30 }
31}
32// Run the program and print any thrown exceptions
33run().catch(console.dir);
1// Delete a document
2
3import { MongoClient } from "mongodb";
4
5// Replace the uri string with your MongoDB deployment's connection string
6const uri = "<connection string uri>";
7
8const client = new MongoClient(uri);
9
10async function run() {
11 try {
12 const database = client.db("sample_mflix");
13 const movies = database.collection("movies");
14
15 /* Delete the first document in the "movies" collection that matches
16 the specified query document */
17 const query = { title: "Annie Hall" };
18 const result = await movies.deleteOne(query);
19
20 /* Print a message that indicates whether the operation deleted a
21 document */
22 if (result.deletedCount === 1) {
23 console.log("Successfully deleted one document.");
24 } else {
25 console.log("No documents matched the query. Deleted 0 documents.");
26 }
27 } finally {
28 // Close the connection after the operation completes
29 await client.close();
30 }
31}
32// Run the program and print any thrown exceptions
33run().catch(console.dir);

참고

동일한 코드 스니펫

위의 JavaScript 및 TypeScript 코드 스니펫은 동일합니다. 이 사용 사례와 관련된 드라이버의 TypeScript 특정 기능은 없습니다.

앞의 예시를 실행하면 다음과 같은 출력이 표시됩니다.

Successfully deleted one document.

예시를 두 번 이상 실행하는 경우 첫 번째 실행에서 일치하는 문서를 삭제했기 때문에 다음과 같은 출력이 표시됩니다.

No documents matched the query. Deleted 0 documents.

돌아가기

삭제 작업

다음

여러 문서 삭제