1.remove删除所有匹配的document

  1. const mongoose = require('mongoose');
  2. mongoose.connect( 'mongodb://127.0.0.1:27017/eggcms', {useNewUrlParser: true});
  3. var UserSchema =new mongoose.Schema({
  4. name:String,
  5. age:Number
  6. })
  7. var User = mongoose.model('User',UserSchema,'user')
  8. User.remove({name:"chengchao"},(err,doc)=>{
  9. if(err) throw err;
  10. console.log(doc)
  11. })

2.deleteOne删除一条document

  1. /* 1.引入mongoose */
  2. const mongoose = require('mongoose');
  3. /* 2.连接数据库 */
  4. mongoose.connect( 'mongodb://127.0.0.1:27017/eggcms', {useNewUrlParser: true});
  5. /* 3.操作user表(集合),定义一个schema */
  6. ;
  7. var UserSchema =new mongoose.Schema({
  8. name:String,
  9. age:Number
  10. })
  11. var User = mongoose.model('User',UserSchema,'user')
  12. User.deleteOne({name:"chengchao"},(err,doc)=>{
  13. if(err) throw err;
  14. console.log(doc)
  15. })