如何替换mongodb中数组字段中的元素 [英] How to replace an element in an array field in mongodb

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

问题描述

字段标签是场景文档中的数组.我想用数组中的好"替换元素坏":

Field tags is array in scenes document. I wanna replace element 'Bad' with 'Good' in the array as:

db.scenes.update({ 'tags': 'Bad' }, { $set: { 'tags.$' : 'Good' } }, { 'multi':true});

我不知道如何在教义上做到这一点.我试过

I don't know how to do it in doctrine. I tried

    $dm->createQueryBuilder('SceneBundle:Scene')
        ->update()
        ->field('tags.$')->set($tag)
        ->field('tags')->equals($oldTag)
        ->multiple(true)
        ->getQuery()
        ->execute();

但不起作用.

谢谢.

推荐答案

已经很长时间了,但是为了没有一个好的答案就不要离开这篇文章,我找到了一个链接 (Mongodb 数组 $push 和 $pull) 可以帮助我们.

it's been a long time, but just to not leave this post without a good answer I found a link (Mongodb array $push and $pull) which can help us.

问题是 MongoDB 不允许在同一更新调用中的相同属性.这意味着两个操作必须发生在两个单独的原子操作中.

The issue is that MongoDB doesn’t allow multiple operations on the same property in the same update call. This means that the two operations must happen in two individually atomic operations.

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

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