【问题标题】:Mongo update field in array based on index基于索引的数组中的Mongo更新字段
【发布时间】:2016-11-10 13:18:33
【问题描述】:

我有一个项目集合 (Projects),记录 project 有一个数组 equipInfo,假设我想更新数组中索引 1 的数量:

"techInfo" : {    
    "equipInfo" : [ 
                {
                    "item" : {
                        "$ref" : "equipment",
                        "$id" : ObjectId("581a20cb4abab607fd17f07d")
                    },
                    "quantity" : "22800",
                    "type" : "module"
                }, 
                {
                    "item" : {
                        "$ref" : "equipment",
                        "$id" : ObjectId("581a20cb4abab607fd17f07e")
                    },
                    "quantity" : "1666",   <===== UPDATE THIS!!!!!!
                    "type" : "inverter"
                }
        ]
}

所以我尝试了:

    Projects.update(
        {_id: projectId},
        {$set: {"techInfo.equipInfo.1.quantity": 1000000}
    );

但这不起作用,我正在使用 Meteor.js 顺便说一句

【问题讨论】:

  • 使用 0 {$set: {"techInfo.equipInfo.1.quantity": 1000000} 检查我的答案
  • 你是在客户端还是在服务器上试了?你收到错误了吗?与您预期的结果不同?正如其他人所提到的,您的语句甚至无法编译(语法错误)。
  • 这是一个虚假的问题吗?看起来接受的答案中的代码与您的原始问题匹配。

标签: mongodb meteor mongodb-query angular-meteor


【解决方案1】:

“这不起作用”是什么意思?有错误吗?

以下内容按预期工作,作为新流星项目中的 /server/main.js:

import { Meteor } from 'meteor/meteor';
import { Mongo  } from 'meteor/mongo'

Projects = new Mongo.Collection("projects");

Meteor.startup(() => {
    Projects.remove({})
    Projects.insert( {
        "techInfo": {
            "equipInfo": [{
                    "item": {
                        "$ref": "equipment",
                        "$id": new Mongo.ObjectID("581a20cb4abab607fd17f07d")
                    },
                    "quantity": "22800",
                    "type": "module"
                },
                {
                    "item": {
                        "$ref": "equipment",
                        "$id": new Mongo.ObjectID("581a20cb4abab607fd17f07e")
                    },
                    "quantity": "1666",
                    "type": "inverter"
                }
            ]
        }
    } )
    let techInfo = Projects.findOne();
    Projects.update({ _id: techInfo._id }, 
      { $set: { "techInfo.equipInfo.1.quantity": 1000000 } }
    );
    console.log(Projects.findOne().techInfo);
});

【讨论】:

    【解决方案2】:

    我认为你需要这个:

    Projects.update(
        {_id: projectId},
        {$set: {"techInfo.equipInfo.1.quantity": 1000000}
    );
    

    【讨论】:

      【解决方案3】:

      如果您只想根据条件更新数组的第一个对象,这就是您的解决方案

      Projects.update({_id: projectId},{$set : {"techInfo.equipInfo.0.quantity" : 1000000}})
      

      如果你想遍历元素找到数组并更新它,你可以使用这个

      Projects.update({_id: projectId,"techInfo.equipInfo" : {"$elemMatch" : {"type" : "module"}}},{$set : {"techInfo.equipInfo.$.quantity" : 1000000}})
      

      【讨论】:

        【解决方案4】:

        这应该可以,$set 之前的 { 需要关闭

        Projects.update(
            {_id: projectId},
            {$set: {"techInfo.equipInfo.1.quantity": 1000000}}
        );
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-06-08
          • 2020-08-21
          • 2014-08-01
          • 2020-05-08
          • 2021-03-15
          • 2011-04-15
          相关资源
          最近更新 更多