我已经编写了一个代码来删除mlabodb上的mongodb帐户中的数据。
router.delete('/user/todo/:item', function(req, res, next){
console.log('Delete request called...');
var data = [{}];
MongoClient.connect(url, function(err, db) {
if (err) throw err;
console.log('connection to mongo established...');
console.log(req.params.item);
var deleteitem = {$pull: {todo: {items:req.params.item}}};
db.collection("users").update({profileID:req.user.profileID}, deleteitem , {multi:true});
if (err) {console.log('something smrthing');}
console.log('run to command...');
res.json(data);
console.log('responding json...');
db.close();
});
});
这是router.js的代码 以下是来自html脚本标签的代码,用于发送ajax请求
$('li').on('click', function(){
var item = $(this).text().replace(/ /g, "-");
$.ajax({
type: 'DELETE',
url: '/user/todo/' + item,
success: function(data){
//do something with the data via front-end framework
location.reload();
}
});
});
数据库看起来像这样
{
"_id": {
"$oid": "595132feeba041141ceb8e5c"
},
"profileID": "1354404264673108",
"fullname": "Siddhant Srivastav",
"profilePic": "https://fb-s-c-a.akamaihd.net/h-ak-fbx/v/t1.0-1/p200x200/18839224_1337818779664990_2672385153328567253_n.jpg?oh=e1b3a2abfa1b4f9792e016eb15f404b2&oe=59DFD8A7&__gda__=1508193289_dabbf65c5a5a4d0f9f826815d5d13eba",
"__v": 0,
"todo": [
{
"item": "something"
}
]
}
我想删除<“item”:“something”>。这段代码没有这样做。 我没有收到任何错误,所有控制台日志都正常工作。