替换数组中的值 [英] Replace value in array

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

问题描述

我正在使用Mongodb C#官方驱动程序.我想用其他值更改数组中的特定值.该数组包含值列表.例如.

I am using Mongodb C# official driver. I want to change a particular value in an array with some other value. The array contains list of values. For eg.

{ 
   _id: id1,
   values: [23, 78, 20]
}

我想用25代替23. 我知道一个答案是否正确. 首先,我拉该元素,然后再推其他元素.但是问题在于,如果不存在该元素,则push也将插入该元素.其次,它将更改位置并在最后插入元素. 请告诉我该怎么做.

I want suppose to replace 23 by 25. I know one answer if it is right. First I pull that element and then push other. But the problem is that if the element is not present then also push will insert the element. Secondly, it will change the position and insert the element at the end. Please tell me how to do it.

推荐答案

您应使用此代码:

db.collection.update({_id: id1, values: 23}, {$set: {'values.$': 25}})

这篇关于替换数组中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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