如何使用findOneAndRemove删除Mongoosejs中的单个子代码?

时间:2015-09-30 21:00:26

标签: node.js mongodb express mongoose

我有像这样的猫鼬模式:

var question = new Schema({
   title : String,
   gambar : String
});

var polling = new Schema({
id_poll : String,
soal : [question]
});

我希望使用像这样的findOneAndRemove方法删除其数组索引所有的subocs,

polling.findOneAndRemove({_id:req.params.poll_id, soal[req.params.soal]._id: req.params.id_soal}, function(err){
if(err) throw err;
console.log("soal deleted");
});

但它会抛出这样的错误:

polling.findOneAndRemove({_id:req.params.poll_id, soal[req.params.soal]._id:
                                                      ^
SyntaxError: Unexpected token [
    at Module._compile (module.js:439:25)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:935:3

所以如何在不知道其索引的情况下删除子网站,我已谷歌它并找到使用运算符$pull但我仍然不明白如何将其应用于我的代码。需要建议和帮助。非常感谢你

1 个答案:

答案 0 :(得分:0)

根据您提供的详细信息,我们有两种可能的方法来删除soal字段:

  1. soal数组设置为[]

    ,清空polling.update({_id: req.params.poll_id }, { $set: { soal: [] }}, function(err, response) { if (err) { console.log('There was an error with your request'); } console.log('Mongo update from running the update query', response); })); 数组
    soal
  2. 从您检索的文档中永久删除polling.update({_id: req.params.poll_id }, { $unset: { soal: "" }}, function(err, response) { if (err) { console.log('There was an error with your request'); } console.log('Mongo update from running the update query', response); })); 属性

    createItem.php
相关问题