尝试更新MongoDb数组元素时出错 [英] Error when trying to update MongoDb array element

查看:124
本文介绍了尝试更新MongoDb数组元素时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Azure CosmosDb MongoApi中,我具有带有嵌入文档数组的JSON.

In my Azure CosmosDb MongoApi I have JSON with an embed array of documents.

{
    "_id": ObjectId("5a95745df886842904b82f71"),
    "token": "value1",
    "channel": "value2",
    "urls":
    [
        {
            "url": "<url1>", 
            "interval": "<int>"
        },
        {
            "url": "<url2>"
            "interval": "<int>"
        }
    ]
}

我想更新特定数组元素中的间隔"字段.问题是当我使用 this ,我最终遇到了一个例外:

I want to update "interval" field in a specific array element. Problem is when I use solutions like this or this, I end up with an exception:

MongoDB.Driver.MongoCommandException: Command findAndModify failed: Invalid BSON field name 'urls.$.interval'.

因此,我决定尝试在Mongo Shell中运行查询并遇到相同的错误:

So, I decided to try running queries in Mongo Shell and got the same error:

{
    "nMatched": 0,
    "nUpserted": 0,
    "nModified": 0,
    "writeError": 
    {
        "code": 2,
        "errmsg": "Invalid BSON field name 'urls.$.interval'"
    }
}

这是我的C#代码:

    var filterBuilder = Builders<User>.Filter;
    var filter = filterBuilder.Where(p => p.Token == model.Token && p.Channel == model.Channel && p.Urls.Any( u => u.Url == model.Url));
    var update = Builders<User>.Update.Set(p => p.Urls.ElementAt(-1).interval, 5);
    await _context.Users.FindOneAndUpdateAsync<User>(filter, update);

这是我的MongoDb shell请求:

Here is my MongoDb shell request:

db.Users.update( {"urls.interval": 60}, {$set: {"urls.$.interval": 30}} ); 

我的问题是导致此异常的原因是什么,我该如何避免呢?

My question is what is causing this exception and how can I avoid it?

推荐答案

Cosmos DB当前不支持位置运算符.请使用以下解决方法:在客户端上遍历文档和数组元素,更改所需的元素,并使用新数组对文档进行更新: 例如,假设您具有以下元素的集合用户:

Positional operator is not currently supported by Cosmos DB. Please use the following workaround: iterate over documents and array elements on the client side, change the required element and issue an update on the document with new array:   For example, assuming you have a collection users of following elements:

{ 
    "_id" : ObjectId("59d6b719708b990d6c9b3aca"), 
    "tags" : [
        {
            "id" : 1, 
            "value" : "11"
        }, 
        {
            "id" : 2, 
            "value" : "22"
        }, 
        {
            "id" : 3, 
            "value" : "32"
        }
    ]
}

 

…您可以发出以下命令来更新其中一个元素(在这种情况下,id = 1):

…you can issue the following command to get one of the elements (with id=1 in this case) updated:  

db.users.find().forEach( 
        function(user) 
        { 
         user.tags.forEach( 
         function(tag) 
         {
               if (tag.id=="1")
               {
                       tag.value = "Updated!";                          
                       db.users.updateOne({_id : user._id}, {$set: {"tags": user.tags}});
               }
        }
        );
       }  
);

您可以在if()中以比位置运算符允许的更好的粒度来调整条件.

You can adjust the condition in if() with even finer granularity than positional operator allows.

这篇关于尝试更新MongoDb数组元素时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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