node 实现删除数据

1.单条删除 -- deleteOne

var MongoClient = require('mongodb').MongoClient;
var url = require('./config/keys').mongoURI;

/**
 * 单条删除
 * deleteOne
*/
MongoClient.connect(url, { useNewUrlParser: true, useUnifiedTopology: true }, function(err, db) {
  if(err) throw err;
  var dbo = db.db('koa'); // koa 库名
  var whereStr = {'name': '测试人员'}; // 查询条件
  dbo.collection('users').deleteOne(whereStr, function(err, res) {
    if(err) throw err;
    console.log('文档删除成功');
    db.close();
  })
});

2.批量删除 -- deleteMany

var MongoClient = require('mongodb').MongoClient;
var url = require('./config/keys').mongoURI;

/**
 * 批量删除
 * deleteMany
*/
MongoClient.connect(url, { useNewUrlParser: true, useUnifiedTopology: true }, function(err, db) {
  if(err) throw err;
  var dbo = db.db('koa'); // koa 库名
  var whereStr = {'age': '20'}; // 查询条件
  dbo.collection('users').deleteMany(whereStr, function(err, res) {
    if(err) throw err;
    console.log(res.deletedCount + ' 条文档删除成功');
    db.close();
  })
});

.

posted @ 2021-06-04 12:00  每天都要进步一点点  阅读(336)  评论(0编辑  收藏  举报