我正试图从下面的对象中删除第二部分。我知道要删除的section_id
对象的section
,下面的代码返回1,好像删除了1条记录,但实际上没有删除section
个对象。
我做错了什么,我该如何解决?
示例对象
{
_id: 'asdfjklsfo',
name: 'My name',
sections: [
{
section_id: 'asdflasdf', <-- This is an actual mongo ObjectID
title: 'My title'
},
{
section_id: 'asdflnmasdf', <-- This is an actual mongo ObjectID
title: 'Second title'
}
]
}
更新代码段
var collection = db.get('notes');
collection.update(
{ _id: id },
{ $pull: { sections: {section_id : section_id}}},
function(err, num_modified) {
// If it failed, return error
if (err) {
res.send("There was a problem deleting that note from the database.");
} else {
console.log(num_modified); <-- this is 1
console.log('were in delete success');
res.send(204);
}
}
);
答案 0 :(得分:0)
我似乎必须这样做collection.id(section_id)
:
collection.update(
{ _id: id },
{ $pull: { sections: {section_id : collection.id(section_id)}}},
function(err, num_modified) {
// If it failed, return error
if (err) {
res.send("There was a problem deleting that note from the database.");
} else {
console.log(num_modified); <-- this is 1
console.log('were in delete success');
res.send(204);
}
}
);
由于我使用的是Monk库,我从中得到了它:https://github.com/LearnBoost/monk#casting