位置$在同一文档的不同部分上工作? [英] Positional $ working on different parts of the same document?

查看:65
本文介绍了位置$在同一文档的不同部分上工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下记录:

r = {
    "_children" : {
        "addressesR" : [
            {
                "street" : "Bitton",
                "city" : "Perth",
                "id" : ObjectId("5317c149f45dfdb114deec41")
            },
            {
                "id" : ObjectId("5317c149f45dfdb114deec42"),
                "street" : "Ivermey",
                "city" : "Perth",
        ],
    },
    "_searchData" : {
        "addressesR" : [
            {
                "street" : "BITTON",
                "city" : "PERTH",
                "id" : ObjectId("5317c149f45dfdb114deec41")
            },
            {
                "id" : ObjectId("5317c149f45dfdb114deec42"),
                "street" : "IVERMEY",
                "city" : "PERTH",
            }
        ],
    },
}

只需保存:

db.p.save( r );

记录包含两个子文档:一个包含子级列表,另一个具有相同的子级列表,但大写(用于搜索).

The record has two sub-documents: one has the list of children, and the other one has the identical list of children but in uppercase (for searching purposes).

当我更新时,我希望能够同时更新两者.现在,这有效:

When I update, I want to be able to update both. Now, this works:

db.p.update( { '$and': [ 
  { '_searchData.addressesR.street': 'BITTON' } ] },
  { '$set': {
    '_searchData.addressesR.$.street':'BITTON CHANGED' ,
    '_children.addressesR.$.street': 'Bitton CHANGED'
  }
})

$运算符是位置运算符.因此,如果位置匹配,它将可以正常工作.但是,如果职位由于任何原因不匹配,我怀疑我将走向灾难.

The $ operator is a positional operator. So, if the positions match, it will work fine. However, if the positions for any reasons mismatch, I suspect I am headed for disasters.

现在,考虑到我总是同时在同一mongoDb查询中同时同时向这两个数组$ push,以及在同一时间从同一个$ pull中拉出它们,我可以接受吗?

Now, considering that I always $push to those two arrays at the same time, and also that I $pull from them at the same time, within the same mongoDb query, should I be OK with this?

在数据库进行复制等环境中,这是否行得通(请参阅:位置始终保持相同)?

Also, is this going to work (see: positions will always always be the same) in an environment where the db has replication going on etc.?

(注意:这是一种自动机制的最终结果,该机制将子记录保存在一条记录中.)

(Note: this is the end result of an automatic mechanism that gets the children records within one record.)

推荐答案

考虑到您的评论,您似乎处在正确的轨道上.如您所知,位置 $ 运算符只是一个值"容器,具有与查询中匹配的第一个数组元素的 index .

Considering you comment you seem to be on the right track. As you are aware, the positional $ operator is just a "value" container, that has the index of the first array element matched in your query.

如果您绝对绝对确保您的两个数组始终包含相同数量的元素,并且这些元素对应条目始终处于同一位置.

You "could" use that if you were absolutely sure that your two arrays always contained the same number of elements and that these corresponding entries were in the same position at all times.

是的,为了安全起见,请使用 $ elemMatch 具有两次这样的更新操作

So yes, in order to be safe, use $elemMatch with two update operations like so

db.p.update( { '$and': [ 
  { '_searchData.addressesR': {$elemMatch: { 'street': 'BITTON' } } ] },
  { '$set': {
    '_searchData.addressesR.$.street':'BITTON CHANGED' ,
  }
})

db.p.update( { '$and': [ 
  { '_children.addressesR': {$elemMatch: { 'street': 'Bitton' } } ] },
  { '$set': {
    '_children.addressesR.$.street': 'Bitton CHANGED'
  }
})

这里唯一真正要考虑的是,由于这两次更新,可能是在两次更新之间可能发生文档的读取并且您的_searchData_children文档在addressesR数组中将没有相同的对应条目.

The only real consideration here is because of the two updates it is possible that a read of the document could occur in between those updates and your _searchData and _children documents would not have the same corresponding entries in the addressesR arrays.

复制中也有同样的事情,因为这两个操作都必须由次级节点重播.

Same thing applies with replication, as both operations have to be replayed by the secordary nodes.

这将是一个不错的功能,那就是能够在查询的 update 部分中使用$ elemMatch.这样,您将可以查询元素在该侧的位置.但这还不存在.但是从2.6开始,您可以执行以下操作:

What would be a nice feature, would be to be able to use $elemMatch in the update part of your query. In that way you would be querying for the position of the element on that side. But this doesn't exist yet. But from 2.6 upwards you can do something like this:

db.runCommand({

    "update": "p",

    "updates": [
        { 
            "q": { '_children.addressesR': {$elemMatch: { 'street': 'Bitton' } },

            "u": { 
                "$set": {
                    "_children.addressesR.$.street": "Bitton CHANGED"
                }
            }
        },
        { 
            "q": { '_searchData.addressesR': {$elemMatch: { 'street': 'BITTON' } },

            "u": { 
                "$set": {
                    "_searchData.addressesR.$.street": "BITTON CHANGED"
                }
            }
        }

    ]
})

该手册页中的批量更新.

这篇关于位置$在同一文档的不同部分上工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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