
- Node.js 教程
- Node.js - 首頁
- Node.js - 簡介
- Node.js - 環境搭建
- Node.js - 第一個應用程式
- Node.js - REPL 終端
- Node.js - 命令列選項
- Node.js - 包管理器 (NPM)
- Node.js - 回撥函式概念
- Node.js - 上傳檔案
- Node.js - 傳送郵件
- Node.js - 事件
- Node.js - 事件迴圈
- Node.js - 事件發射器
- Node.js - 偵錯程式
- Node.js - 全域性物件
- Node.js - 控制檯
- Node.js - 程序
- Node.js - 應用程式擴充套件
- Node.js - 打包
- Node.js - Express 框架
- Node.js - RESTful API
- Node.js - 緩衝區
- Node.js - 流
- Node.js - 檔案系統
- Node.js MySQL
- Node.js - MySQL 快速入門
- Node.js - MySQL 建立資料庫
- Node.js - MySQL 建立表
- Node.js - MySQL 插入資料
- Node.js - MySQL 查詢資料
- Node.js - MySQL 條件查詢
- Node.js - MySQL 排序
- Node.js - MySQL 刪除資料
- Node.js - MySQL 更新資料
- Node.js - MySQL 連線查詢
- Node.js MongoDB
- Node.js - MongoDB 快速入門
- Node.js - MongoDB 建立資料庫
- Node.js - MongoDB 建立集合
- Node.js - MongoDB 插入資料
- Node.js - MongoDB 查詢資料
- Node.js - MongoDB 查詢
- Node.js - MongoDB 排序
- Node.js - MongoDB 刪除
- Node.js - MongoDB 更新資料
- Node.js - MongoDB 資料限制
- Node.js - MongoDB 連線查詢
- Node.js 模組
- Node.js - 模組
- Node.js - 內建模組
- Node.js - 實用工具模組
- Node.js - Web 模組
- Node.js 有用資源
- Node.js - 快速指南
- Node.js - 有用資源
- Node.js - 討論
Node.js - MongoDB 刪除
Node.js 的 mongodb 驅動程式在 Collection 類中包含兩種方法。deleteOne() 方法刪除一個文件,而 deleteMany() 方法用於一次刪除多個文件。這兩種方法都需要一個 filter 引數。
collection.deleteOne(filter);
請注意,如果有多個文件滿足給定的過濾器條件,則只刪除第一個文件。
deleteOne()
在下面的例子中,deleteOne() 方法從 products 集合中刪除一個 name 欄位匹配 TV 的文件。
const {MongoClient} = require('mongodb'); async function main(){ const uri = "mongodb://:27017/"; const client = new MongoClient(uri); try { await client.connect(); await deldocs(client, "mydb", "products"); } finally { await client.close(); } } main().catch(console.error); async function deldocs(client, dbname, colname){ var myqry = { Name: "TV" }; const result = await client.db(dbname).collection(colname).deleteOne(myqry); console.log("Document Deleted"); }
執行上述程式碼後,使用 MongoCompass(或 MongoShell)驗證預期的文件是否已刪除。
deleteMany()
deleteMany() 方法也使用 filter 引數。但是,它會導致所有滿足指定條件的文件都被刪除。
在上面的程式碼中,將 deldocs() 函式更改如下。這將導致所有 price>10000 的文件被刪除。
async function deldocs(client, dbname, colname){ var myqry = {"price":{$gt:10000}}; const result = await client.db(dbname).collection(colname).deleteMany(myqry); console.log("Documents Deleted"); }
刪除集合
您可以使用 drop() 方法從資料庫中刪除集合。
示例
const {MongoClient} = require('mongodb'); async function main(){ const uri = "mongodb://:27017/"; const client = new MongoClient(uri); try { await client.connect(); await dropcol(client, "mydb", "products"); } finally { await client.close(); } } main().catch(console.error); async function dropcol(client, dbname, colname){ const result = await client.db(dbname).collection(colname).drop(); console.log("Collection dropped "); }
db 物件中還有一個可用的 **dropCollection()** 方法。
const {MongoClient} = require('mongodb'); async function main(){ const uri = "mongodb://:27017/"; const client = new MongoClient(uri); try { await client.connect(); await dropcol(client, "mydb", "orders"); } finally { await client.close(); } } main().catch(console.error); async function dropcol(client, dbname, colname){ const result = await client.db(dbname).dropCollection(colname); console.log("Collection dropped"); }
廣告