Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs Menu
Docs Home
/ / /
Node.js ドライバー
/ /

ドキュメントの削除

collection.deleteOne() を使用して、コレクション内の 1 つのドキュメントを削除できます。deleteOne() メソッドは、指定したクエリ ドキュメントを使用して、クエリに一致するコレクション内のドキュメントのサブセットを照合します。クエリ ドキュメントを指定しない場合(または空のドキュメントを指定した場合)、MongoDB はコレクション内のすべてのドキュメントを照合し、最初に一致したドキュメントを削除します。

メソッドの 2 番目のパラメータとして渡される オブジェクトを使用して、さらにクエリ オプションを指定できます。このメソッドの詳細については、optionsdeleteOne deleteOne() APIドキュメントを参照してください。

注意

アプリケーションで削除後に削除されたドキュメントが必要な場合は、コレクションの.findOneAndDelete() deleteOne()メソッドの使用を検討してください。このメソッドは と同様のインターフェースを持ちますが、削除されたドキュメントも返します。

次のスニペットは、 movies コレクションから 1 つのドキュメントを削除します。これは、 title値が "Annie Hall" となる映画に一致するようにクエリを構成するクエリ ドキュメントを使用します。

注意

この例を使用して、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.

戻る

削除