
- Node 和 MongoDB 教程
- Node 和 MongoDB - 主頁
- Node 和 MongoDB - 概述
- Node 和 MongoDB - 環境設定
- Node 和 MongoDB 示例
- Node 和 MongoDB - 連線資料庫
- Node 和 MongoDB - 顯示資料庫
- Node 和 MongoDB - 刪除資料庫
- Node 和 MongoDB - 建立集合
- Node 和 MongoDB - 刪除集合
- Node 和 MongoDB - 顯示集合
- Node 和 MongoDB - 插入文件
- Node 和 MongoDB - 選擇文件
- Node 和 MongoDB - 更新文件
- Node 和 MongoDB - 刪除文件
- Node 和 MongoDB - 嵌入式文件
- Node 和 MongoDB - 限制記錄
- Node 和 MongoDB - 排序記錄
- Node 和 MongoDB 有用資源
- Node 和 MongoDB - 快速指南
- Node 和 MongoDB - 有用資源
- Node 和 MongoDB - 討論
Node 和 MongoDB - 刪除文件
要刪除集合中的文件,可以使用collection.deleteOne()或collection.deleteMany()方法來刪除一個或多個文件。
database.collection("sampleCollection").deleteOne(query, function(error, result) { if (error) throw error; console.log('Document deleted.'); }); database.collection("sampleCollection").deleteMany(query, function(error, result) { if (error) throw error; console.log(result.result.n + " document(s) deleted."); });
示例
嘗試以下示例在 mongodb 集合中刪除文件-
複製並將以下示例貼上為 mongodb_example.js-
const MongoClient = require('mongodb').MongoClient; // Prepare URL const url = "mongodb://:27017/"; // make a connection to the database MongoClient.connect(url, function(error, client) { if (error) throw error; console.log("Connected!"); // Connect to the database const database = client.db('myDb'); database.collection("sampleCollection").deleteOne({First_Name:'Mahesh'}, function(error, result) { if (error) throw error; console.log('Document Deleted.'); }); // close the connection client.close(); });
輸出
使用 node 執行 mysql_example.js 指令碼並驗證輸出。
node mongodb_example.js Connected! Document Deleted.
廣告