Node.js MongoDB删除

在MongoDB中, 你可以使用remove()方法删除记录或文档。 remove()方法的第一个参数是一个查询对象, 它指定要删除的文档。

例子

删除地址为Ghaziabad的员工的记录。

创建一个名为” remove.js”的js文件, 其代码如下:

var http = require('http');
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/ MongoDatabase";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var myquery = { address: 'Ghaziabad' };
db.collection("employees").remove(myquery, function(err, obj) {
if (err) throw err;
console.log(obj.result.n + " record(s) deleted");
db.close();
});
});

打开命令终端并运行以下命令:

Node remove.js
Node.js删除1

验证

你可以检查是否删除了地址为” Ghaziabad”的记录, 并且现在只有以下记录可用:

Node.js删除2

来源:

https://www.srcmini02.com/27886.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_38374.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?