collection.deleteOne()
を使用して、コレクション内の 1 つのドキュメントを削除できます。deleteOne()
メソッドは、指定したクエリ ドキュメントを使用して、クエリに一致するコレクション内のドキュメントのサブセットを照合します。クエリ ドキュメントを指定しない場合(または空のドキュメントを指定した場合)、MongoDB はコレクション内のすべてのドキュメントを照合し、最初に一致したドキュメントを削除します。
メソッドの 2 番目のパラメータとして渡される オブジェクトを使用して、さらにクエリ オプションを指定できます。このメソッドの詳細については、options
deleteOne
deleteOne() APIドキュメントを参照してください。
注意
アプリケーションで削除後に削除されたドキュメントが必要な場合は、コレクションの.findOneAndDelete() deleteOne()
メソッドの使用を検討してください。このメソッドは と同様のインターフェースを持ちますが、削除されたドキュメントも返します。
例
次のスニペットは、 movies
コレクションから 1 つのドキュメントを削除します。これは、 title
値が "Annie Hall" となる映画に一致するようにクエリを構成するクエリ ドキュメントを使用します。
注意
この例を使用して、MongoDB のインスタンスに接続し、サンプルデータを含むデータベースと交流できます。MongoDB インスタンスへの接続とサンプルデータセットの読み込みの詳細については、 使用例ガイドを参照してください。
1 // Delete a document 2 3 import { MongoClient } from "mongodb"; 4 5 // Replace the uri string with your MongoDB deployment's connection string 6 const uri = "<connection string uri>"; 7 8 const client = new MongoClient(uri); 9 10 async 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 33 run().catch(console.dir);
1 // Delete a document 2 3 import { MongoClient } from "mongodb"; 4 5 // Replace the uri string with your MongoDB deployment's connection string 6 const uri = "<connection string uri>"; 7 8 const client = new MongoClient(uri); 9 10 async 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 33 run().catch(console.dir);
注意
同一のコードスニペット
上記の JavaScript と TypeScript のコード スニペットは同一です。このユースケースに関連するドライバーの TypeScript 固有の機能はありません。
前の例を実行すると、次の出力が表示されます。
Successfully deleted one document.
この例を複数回実行すると、最初の実行で一致したドキュメントが削除されるため、次の出力が表示されます。
No documents matched the query. Deleted 0 documents.