Javascript 删除';注';在Node.js Express中的mongoDB集合中
我想知道如何从Node.js中的mongoDB中删除“note” 我不熟悉Node.js、Express和mongoDB 我会在这里显示我的代码,让它自己说话 notesController.js:Javascript 删除';注';在Node.js Express中的mongoDB集合中,javascript,angularjs,node.js,mongodb,api,Javascript,Angularjs,Node.js,Mongodb,Api,我想知道如何从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 ca
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);
}
});
});
数据文件夹中的index.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();
});
...
您应该使用更新
,例如
...
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,它为您的示例文档演示了这一点。当然,您必须确保从请求数据中正确设置了
注意
我认为
...
db.notes.remove({ note: note }, function(err, results) {
console.log(results); //not too sure what to do here?
next();
});
...
您应该使用更新
,例如
...
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,它为您的示例文档演示了这一点。当然,您必须确保从请求数据中正确设置了
注意
如果我理解正确,那么您只想删除数组中的元素,而不是整个文档,对吗?是的,完全正确:)如果我不清楚,对不起@DAXaholicSo如果我理解正确,那么您只想删除数组中的元素,而不是整个文档,对吗?是的,完全正确:)如果我不清楚,对不起@达克斯霍里谢谢你!真是太棒了!还有一件事。在客户端上,我有这样一个角度代码:$scope.removeNote=function(note){$http.put(notesUrl+'/'+note)。然后(function(result){$scope.notes=result.data;},function(err){console.log(err);};它可以工作,但在页面上看起来很奇怪,所以我必须刷新它。当我插入一个新的时,我有类似的情况。当我插入一个新的时,便笺会显示出来,而不会刷新页面。在这里也能实现吗@达克斯霍里谢谢你!真是太棒了!还有一件事。在客户端上,我有这样一个角度代码:$scope.removeNote=function(note){$http.put(notesUrl+'/'+note)。然后(function(result){$scope.notes=result.data;},function(err){console.log(err);};它可以工作,但在页面上看起来很奇怪,所以我必须刷新它。当我插入一个新的时,我有类似的情况。当我插入一个新的时,便笺会显示出来,而不会刷新页面。在这里也能实现吗@达斯霍利克