【问题标题】:Mongoose: Can't work out how to update value in nested object arrayMongoose:无法弄清楚如何更新嵌套对象数组中的值
【发布时间】:2021-11-11 00:57:06
【问题描述】:

我正在尝试解决如何更新位于对象数组中的特定对象值。 我可能错过了答案或无法正确执行答案,但由于我正在尝试解决一段时间,因此我们将不胜感激。

我有一个名为 todoListDB 的 Mongo 数据库,里面有一个名为列表的集合。 列表中的每个文档都是一个待办事项列表。 每个文档都有一个列表名称和一个项目数组。 数组中的每个项目都是一个任务。 每个任务对象都有一个名称和一个检查值。 我希望能够将选中的值从 0 更新为 1。

这里是一些我尝试过的代码示例。

// todoListDB
// lists collection: each document is a singel todo list
// items: each array object has a task and a checkbox state value

// example of path structure
{todoListDB:{lists:{

// todo list
{_id: {mongoose_id}, name: "shool", items:[
  {name: "do homework", checked: 0, _id:{mongoose_id}},
  {name: "read book", checked: 0, _id:{mongoose_id}},
]},
// todo list
{_id: {mongoose_id}, name: "work", items:[
  {name: "fix this", checked: 0, _id:{mongoose_id}},
  {name: "fix that", checked: 0, _id:{mongoose_id}},
]}
}}}

// update checkbox value checked:
// from:
{name: "do homework", checked: 0, _id:{mongoose_id}}
// to:
{name: "do homework", checked: 1, _id:{mongoose_id}}

let listName = 'school';
// I can query the array items but can't change the value
List.findOne({name: listName}, async function(err, foundList){
  if (!err){
    let items = foundList.items;
    for (let i = 0; i < items.length; ++i) {
      await delayed(100);
      // Working
      console.log('CHECKED:', items[i].name, items[i].checked);

      // Doesn't work, no errors and no changes to the checked value
      items[i].checked = 1;
      // Doesn't work, no errors and no changes to the checked value
      foundList.updateOne({name:items[i].name},{checked:1});
    }
  }
});

// Doesn't work, no errors and no changes to the checked value
List.findByIdAndUpdate({_id : id}, {checked:1},{overwrite:true}, function(err, doc){
    if (err){
      console.log(err);
    }
    else{
      console.log('UPDATED: ', doc)
    }
});

// Doesn't work: bad value error
let update = {'$set':{'items.$.name': 'eat','items.$.checked':1}}
List.findOneAndUpdate({name:listName},update,function(err, foundList){
  if (err){
    console.log(err);
  }
});

// Working code from tutorial to delete items from array, but can't figure out how to write/convert
// the "path" to the object value I want to change using $set
List.findOneAndUpdate({name:listName},{$pull:{items: {name:subjects[i]}}},function(err, foundList){
  if (err){
    console.log(err);
  }
});

【问题讨论】:

    标签: javascript node.js arrays mongodb


    【解决方案1】:

    我已经能够解决我自己的问题。诀窍似乎是您必须使用数组过滤器。

    // Query document with name school //
    let query = {name:'school'};
    // Update found object in items arary, set object.checked value to 1 //
    let update = {'$set':{'items.$[elem].checked':1}};
    // Find in (items) array object to be updated  with key value pair: name:do homework //
    let arrayFilter = {arrayFilters:[{'elem.name':'do homework'}]};
    
    List.findOneAndUpdate(query, update, arrayFilter,function(err, foundList){
      if (err){
        console.log(err);
      }
    });
    
    

    【讨论】:

      猜你喜欢
      • 2013-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-31
      • 1970-01-01
      • 2018-06-04
      • 2019-03-12
      相关资源
      最近更新 更多