mongoose 常用數據庫操作 刪除


 

刪除

 

  Model.remove(conditions, [callback])

try.js

var User = require("./user.js");

function del(){
    var wherestr = {'username' : 'zhangming'};

    User.remove(wherestr, function(err, res){
        if (err) {
            console.log("Error:" + err);
        }
        else {
            console.log("Res:" + res);
        }
    })
}

del();

刪除前robo的數據庫:

執行刪除:

在webstorm的輸出結果:

 

 

在rob看到刪除成功:

 

Model.findByIdAndRemove(id, [options], [callback]) 

 

try.js

var User = require("./user.js");

function findByIdAndRemove()   {
    var id = '59f9eb4cc2283e1bac7be51d';
    var removestr = {'userage': '30'};

    User.findByIdAndRemove(id,  removestr, function(err, res){
        if (err) {
             console.log("Error:" + err);
        }
        else {
            console.log("Res:" + res);
        }
    })
}

findByIdAndRemove();

刪除前;

 

刪除后:

在robo中

刪除成功!

 在webstrom 的輸出結果:

 

 

 

 

 Model.findOneAndRemove(conditions, [options], [callback])

try.js

var User = require("./user.js");

function findOneAndRemove()   {

    var removestr = {'userage': '17'};

    User.findOneAndRemove(removestr, function(err, res){
        if (err) {
             console.log("Error:" + err);
        }
        else {
            console.log("Res:" + res);
        }
    })
}
findOneAndRemove();

 

在webstrom 輸出結果:

 

 

 

刪除前的數據庫:

 

刪除后的數據庫:

 2017-11-02  10:28:00


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM