0

这是我的收藏:

{
    "_id" : "Kan6btPXwNiF84j8e",
    "title" : "Chapter Title 1",
    "businessId" : "qmWJ3HtZrpka8dpbM",
    "createdBy" : "GfdPfoPTiSwLv8TBR",
    "sections" : [ 
        {
            "id" : "T5KAfTcCb7pCudT3a",
            "type" : "TEXT",
            "data" : {
                "text" : "<h1>2</h1><h1>asd</h1>"
            },
            "createdAt" : ISODate("2016-12-03T10:35:59.023Z"),
            "updatedAt" : ISODate("2016-12-03T10:35:59.023Z")
        }
    ],
    "createdAt" : ISODate("2016-12-02T12:15:16.577Z"),
    "updatedAt" : ISODate("2016-12-03T12:54:50.591Z")
}

这是我从客户端调用的流星方法

deleteSection: function (section_id, chapter_id) {


        chaptersCollection.update(
            {$and: [{_id: chapter_id}, {'sections.id': section_id}]},
            {$pull: {'sections': {'id': section_id}}},
            function (err, numAffected) {
                if (err) {
                    console.log(err);
                    return err;
                }else{
                    console.log(numAffected);
                }
            });
        return 'Section Successfully Deleted';
    }

在meteor方法的回调函数中,它返回1作为受影响的行。但是在服务器文档上没有更新。

有什么建议我哪里错了吗?

4

2 回答 2

0

你真的需要$and吗?

deleteSection: function (section_id, chapter_id) {
    chaptersCollection.update(
        {_id: chapter_id, 'sections.id': section_id},
        {$pull: {'sections': {'id': section_id}}},
        function (err) {
            if (err) {
                console.log(err);
                return err;
            }else{
                console.log('success');
                return 'success';
            }
        });
}
于 2016-12-03T13:08:20.310 回答
0

当我尝试在项目中使用 pull 时,我遇到了类似的问题。因此,我没有使用 $pull,而是在数据库外部处理数组,然后将数组设置为我在外部处理的数组。因此,也许您可​​以尝试类似的方法作为替代方法

deleteSection: function (section_id, chapter_id){

     const oldArray = chaptersCollection.findOne(chapter_id).sections;
     const newArray = oldArray.filter(function(section) {
                         return section.id !== section_id
                         });

     chaptersCollection.update({_id: chapter_id},
                               {$set: {sections: newArray}},
                               function (err, numAffected) {
                                   if (err) {
                                       console.log(err);
                                       return err;
                                    }else{
                                       console.log(numAffected);
                                    }
                                });
     return 'Section Successfully Deleted';

}
于 2016-12-04T11:03:54.093 回答