简体   繁体   English

C# MongoDB pullFilter 从字符串数组中删除字符串

[英]C# MongoDB pullFilter to remove string from string array

I am trying to do a pullFilter and can get it to work on complex types.我正在尝试做一个 pullFilter 并且可以让它在复杂类型上工作。

            await _Collection.UpdateOneAsync(
            Builders<Descriptor>.Filter.Eq(d => d.Id, id),
            Builders<Descriptor>.Update
                .Set(d => d.UpdatedBy, actioner)
                .Set(d => d.UpdatedOn, DateTime.Now)
                .PullFilter(d => d.Options, "Remove this one")
            );

However, Options is an array of string values and I cannot get it to remove the value "Remove this one":但是,Options 是一个字符串值数组,我无法让它删除值“Remove this one”:

{
    "Name" : "Test",
    "Type" : NumberInt(1),
    "Options" : [
        "Testing",
        "Tested",
        "Remove this one"
    ]
}

This is my error message:这是我的错误信息:

message": "JSON reader was expecting a value but found 'Remove'."

I also tried with this:我也试过这个:

        await _Collection.UpdateOneAsync(
            Builders<Descriptor>.Filter.Eq(d => d.Id, id),
            Builders<Descriptor>.Update
                .Set(d => d.UpdatedBy, actioner)
                .Set(d => d.UpdatedOn, DateTime.Now)
                .PullFilter(d => d.Options, d => d == "Remove this one")
        );

Which results in this error:导致此错误:

"message": "{document} is not supported."

You should use UpdateDefinitionExtensions.Pull<TDocument, TItem> Method (UpdateDefinition, FieldDefinition, TItem) instead of .PullFilter() .您应该使用UpdateDefinitionExtensions.Pull<TDocument, TItem> Method (UpdateDefinition, FieldDefinition, TItem)而不是.PullFilter()

await _Collection.UpdateOneAsync(
    Builders<Descriptor>.Filter.Eq(d => d.Id, id),
    Builders<Descriptor>.Update
        .Set(d => d.UpdatedBy, actioner)
        .Set(d => d.UpdatedOn, DateTime.Now)
        .Pull(d => d.Options, "Remove this one")
);

Demo演示

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM