如何访问值对象数组的MongoDB findAndModify [英] How to access value in object in array Mongodb findAndModify

查看:932
本文介绍了如何访问值对象数组的MongoDB findAndModify的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更新数组内的对象中的值。我会提供一个可变的确切序列号...我的继承人code:

  VAR NUM = 0;
VAR对象ID =要求('mongodb的')对象ID。db.collection('投票')。findAndModify({
    查询:{_id:对象ID(_id参数)},
    更新时间:{$ INC:{总:1,Data.chart。 + num.toString +.value的:1}}
});

这是我想更新什么

  {总:0,数据:
  {值:0,标签:'披头士',颜色:#4169E1},
  {值:0,标签:'刺',颜色:#C0C0C0},
  {值:0,标签:'警察',颜色:#FFA500},
  {值:0,标签:'西游记',颜色:#FF4500},
  {值:0,标签:'创世纪',颜色:#EE82EE'}
  ]
}


解决方案

JavaScript的stringifiespresented键,所以这就是为什么你没有得到你所期望的。

要使用一个变量作为计算的键值名的一部分,你需要使用括号 [] 符号来定义对象:

VAR NUM = 0;变种更新= {$增量:{总:1}};更新[$ INC] [数据。 + NUM +.value的] = 1;db.collection('投票')。findAndModify({
    查询:{_id:对象ID(_id参数)},
    更新:更新
});

更重要的是,你不应该presume的指数,你认为你要修改的是数据的实际位置。相反,数组元素在查询相匹配,并使用位置 $ 操盘手硬指数:

db.collection('民意调查')。findAndModify({
    查询:{_id:对象ID(_idParam),Data.label:'披头士'},
    更新:{$ INC:{总:1,数据$价值。:1}}
});

所以匹配 Data.label 将在一个更新的且不论甲壳虫,元素数组中的当前索引位置。

这意味着可升级,而其他写入可能会改变数组的内容,但你还是更新你想要的,即使改变了位置,由于另一个更新。

NB 在你的问题。文件为数据,而不是路径数组元素 Data.chart 。但是,但是应用的实际数据池莉构建。

How do I update a value inside a object inside an array. I will provide the exact array number in a variable... Heres my code:

var num = 0;
var ObjectID=require('mongodb').ObjectID;

db.collection('polls').findAndModify({
    query: {_id: ObjectID(_id param)},
    update: { $inc: { total: 1, "Data.chart." + num.toString + ".value": 1} }
});

This is what I want to update

{total: 0, "Data": [ 
  { value: 0, label: 'Beatles', color: '#4169E1' },
  { value: 0, label: 'Sting', color: '#C0C0C0' },
  { value: 0, label: 'Police', color: '#FFA500' },
  { value: 0, label: 'Journey', color: '#FF4500' },
  { value: 0, label: 'Genesis', color: '#EE82EE' } 
  ]
}

解决方案

JavaScript "stringifies" presented keys, so this is why you are not getting what you expect.

To use a variable as part of a "calculated key name" you need to use the bracket [] notation to define the object:

var num = 0;

var update = { "$inc": { "total": 1 } };

update["$inc"]["Data." + num + ".value"] = 1;

db.collection('polls').findAndModify({
    query: { _id: ObjectID(_id param) },
    update: update
});

Better yet, you should not presume the "index" that you think you want to modify is the actual location of the data. Instead match the array element in the query and use the positional $ operator instead of a hard index:

db.collection('polls').findAndModify({
    query: { "_id": ObjectID(_idParam), "Data.label": 'Beatles'   },
    update: { "$inc": { "total": 1, "Data.$.value": 1 } }
});

So the element that matches Data.label as "Beatles" will be the one updated, and regardless of the current index position in the array.

That means "scalable", where other writes could alter the array content but you are still updating the one you want, even if it changed position due to another update.

N.B The document in your question has the path to array elements as Data and not Data.chart. But apply however your real data is contructed.

这篇关于如何访问值对象数组的MongoDB findAndModify的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆