簡體   English   中英

從Node.js Express中的mongoDB中刪除集合中的'note'

[英]Delete 'note' in collection from mongoDB in Node.js Express

我想知道如何從Node.js中的mongoDB中刪除'note'

我是Node.js,Express和mongoDB的新手。

我只是在這里展示我的代碼並讓它代表自己......

notesController.js:

    app.delete('/api/notes/:categoryName/:note', 
   // auth.ensureApiAuthenticated, commented when testing.
     function(req, res) {

        var categoryName = req.params.categoryName;
        var note = req.params.note;

        data.removeNote(categoryName, note, function(err, notes){
            if(err) {
                res.send(400, err);
            }
            else {
                res.send(200);
            }
        });
    });

data.js來自數據文件夾:

     data.removeNote = function(categoryName, note, next) {
    database.getDb(function(err, db){
        if(err) {
            next(err);
        }
        else {
            db.notes.remove({ note: note }, function(err, results) {
                console.log(results); //not too sure what to do here?
                next();
            });
        }
    });
};

MongoDB:這是我試圖刪除的注釋。 不編程,因為那是類別。

{
  "name": "Programming",
  "notes": [
    {
      "note": "Fiddler Note",
      "color": "blue",
      "author": "oOMelon"
    }
  ]
}

我在測試時使用Fiddler,我得到200作為狀態代碼...但它不會從數據庫中刪除。 請幫忙! :)

提前致謝 :)

我想而不是

...
db.notes.remove({ note: note }, function(err, results) {
    console.log(results); //not too sure what to do here?
    next();
});
...

你應該使用$ pull update ,即類似這樣的東西

...
db.notes.update({ "notes.note": note }, 
                { $pull: { notes: { note: note } } }, 
                function(err, results) {

    console.log(results); //not too sure what to do here?
    next();
});
...

請參閱以下GIF,其中說明了您的示例文檔。
當然,您必須確保從請求數據中正確設置了note

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM