【问题标题】:How to update a array value in Mongoose如何在 Mongoose 中更新数组值
【发布时间】:2017-05-20 23:51:37
【问题描述】:

我想更新一个数组值,但我不确定执行此操作的正确方法,所以我尝试了以下方法但对我没有用。

我的模特, 我的模型中的 children 字段

   childrens: {
       type: Array,
       default: ''
  }

我的查询,

   Employeehierarchy.update({ _id: employeeparent._id} ,{ $set: {"$push": { "childrens": employee._id }} })
   .exec(function (err, managerparent) {});

谁能帮帮我。谢谢。

【问题讨论】:

    标签: node.js mongodb express mongoose mean-stack


    【解决方案1】:

    您不能在与嵌套运算符相同的更新表达式中同时使用 $set$push

    使用update operators 的正确语法如下:

    {
       <operator1>: { <field1>: <value1>, ... },
       <operator2>: { <field2>: <value2>, ... },
       ...
    }
    

    其中&lt;operator1&gt;, &lt;operator2&gt; 可以来自指定here 的任何更新运算符列表。

    要向数组中添加新元素,单个 $push 运算符就足够了,例如您可以使用 findByIdAndUpdate 更新方法将修改后的文档返回为

    Employeehierarchy.findByIdAndUpdate(employeeparent._id,
        { "$push": { "childrens": employee._id } },
        { "new": true, "upsert": true },
        function (err, managerparent) {
            if (err) throw err;
            console.log(managerparent);
        }
    );
    

    使用你原来的 update() 方法,语法是

    Employeehierarchy.update(
       { "_id": employeeparent._id},
       { "$push": { "childrens": employee._id } },
       function (err, raw) {
           if (err) return handleError(err);
           console.log('The raw response from Mongo was ', raw);
       }
    );
    

    其中回调函数接收参数(err, raw) where

    • 如果发生任何错误,err 就是错误
    • raw 是 Mongo 的完整回复

    由于您要查看修改后的文档,我建议您使用 findByIdAndUpdate 函数,因为 update() 方法不会给您修改后的文档文档,只是 mongo 的完整写入结果。


    如果您想更新文档中的字段并同时将元素添加到数组中,那么您可以这样做

    Employeehierarchy.findByIdAndUpdate(employeeparent._id,
        { 
            "$set": { "name": "foo" },
            "$push": { "childrens": employee._id } 
        } 
        { "new": true, "upsert": true },
        function (err, managerparent) {
            if (err) throw err;
            console.log(managerparent);
        }
    );
    

    上面会将name字段更新为“foo”并将员工ID添加到childrens数组中。

    【讨论】:

    • 如果我测试经理打印 console.log(managerparent+'kk') 我收到错误为 uncaughtException: managerparent is not defined
    • Thsnks chridam it wrkd.
    • 我还有一个问题要问你:),如果你没事的话。
    【解决方案2】:

    可以关注这个

    如果childrens 包含字符串值,那么模型可以是:

    childrens: [{
        type : String
    }]
    

    如果 childrens 包含另一个集合 _id 的 ObjectId 值并且想要填充,那么模型可以是:

    childrens: [{
        type : mongoose.Schema.Types.ObjectId,
        ref: 'refModelName'
    }]
    

    无需使用$set 只需使用$push 将值插入childrens 数组。所以查询可以是:

    Employeehierarchy.update(
       { _id: employeeparent._id},
       {"$push": { "childrens": employee._id } }
     ).exec(function (err, managerparent) {
        //
     });
    

    【讨论】:

    • I 模型被声明,所以它可能成为对象数组?我不想要的
    • 不是对象数组将是包含 ids 的数组(作为第一个进程的字符串,第二个进程作为 ObjectId)@nlr_p
    【解决方案3】:

    这会帮助我猜

    Employeehierarchy.findOneAndUpdate(
      { _id:employeeparent._id },
      { $set: { "childrens": employee._id }}
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-19
      • 2020-10-09
      • 1970-01-01
      • 2018-11-28
      • 2020-10-09
      • 2020-09-13
      • 2015-09-19
      相关资源
      最近更新 更多