MongoDB C#驱动程序在数组为空时更新所有子数组元素失败 [英] Mongodb C# driver update all subarray elements failed when array is empty

查看:80
本文介绍了MongoDB C#驱动程序在数组为空时更新所有子数组元素失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

链接到这篇文章我尝试并能够更新文档的所有子数组元素.总结一下,我想使用 $ C#中的[] 运算符

Linked to this post I tried and was able to update all sub-array elements of a document. To sum up I want to use the $[] operator in C#

这是我的存储库中的更新请求:

Here is the update request in my repository :

var date = DateTime.UtcNow;
update = update.Set(x => x.LastUpdateDate, date);
update = update.Set(x => x.EndDate, date);
update = update.Set("Quotes.$[].DraftStatus", Constants.ProjectCloseStatus);

var res = _mongoCollection.UpdateMany(filter, update);

当"Quotes"数组不为空时,它工作得很好. 不幸的是,当数组不存在时,我在数据库中得到以下输出:

It is working very well when the "Quotes" array is not empty. Unfortunately when the array does not exist, I got the following output in the DB :

{
    "_id" : ObjectId("5df73ac41043a04ee0873253"),
    "Quotes" : {
        "$[]" : {
            "DraftStatus" : "CLOSE"
        }
    }
}

由于解析,读取操作失败. 进行更新时,我遇到以下问题:

And the read operation is failed because of the parse. When I proceed the update I have the following issue :

写操作导致错误.路径"Quotes"必须存在 在文档中以应用数组更新.

A write operation resulted in an error. The path 'Quotes' must exist in the document in order to apply array updates.

像错误消息一样,

推荐答案

,只允许您在作为数组的字段上运行数组更新操作.如果该字段的值为null或不存在,则更新也将失败.

like the error message says, you are only allowed to run array update operations on a field that is an array. if the field has a value of null or does not exist, the update will also fail.

最简单的解决方案是在保存文档时使该字段为空数组,以使其在db中看起来像这样:

the simplest solution is to make the field an empty array when saving the documents so that it looks like this in the db:

{
    "_id": ObjectId("5df9af0e22bb051d0c25c936"),
    "Quotes": [ ]
}

可以通过为您的c#属性赋予默认值来轻松实现,如下所示:

which can be easily achieved by giving your c# property a default value like so:

    public Quote[] Quotes { get; set; } = new Quote[0];

测试程序:

using MongoDB.Entities;
using MongoDB.Entities.Core;

namespace StackOverflow
{
    public class Test : Entity
    {
        public string Name { get; set; }
        public Quote[] Quotes { get; set; } = new Quote[0];
    }

    public class Quote
    {
        public bool flag { get; set; }
        public string status { get; set; }
    }

    public class Program
    {
        private static void Main(string[] args)
        {
            new DB("test", "localhost");

            (new[] {
                new Test { Name = "no quotes"},
                new Test { Quotes = new[]{
                    new Quote { flag = true, status = "PROCESSED" } } },

                new Test { Quotes = new[]{
                    new Quote { flag = true, status = "NOT-PROCESSED" },
                    new Quote { flag = true, status = "NOT-PROCESSED" }
                }}
            }).Save();

            var field = Prop.PosAll<Test>(t => t.Quotes[0].flag);

            DB.Update<Test>()
              .Match(_ => true)
              .Modify(b => b.Set(field, false))
              .Execute();
        }
    }
}

这篇关于MongoDB C#驱动程序在数组为空时更新所有子数组元素失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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