我有一个包含以下文档的集合:

{
    "_id": ObjectId("507f191e810c19729de860ea"),
    "Tags": [
        "tag1",
        "tag2"
    ]
    "SubCollection": [{
        "_id": ObjectId("55849c0002cee826cc67550a"),
        "Timestamp": NumberLong(635703508937961307)
    },
    {
        "_id": ObjectId("507f1f77bcf86cd799439011"),
        "Timestamp": NumberLong(635703508937961307)
    }]  
}

    public class RootEntity
    {
        public ObjectId Id { get; set; }
        public List<string> Tags { get; set; }
        public List<SubEntity> SubCollection { get; set; }
    }

    public class SubEntity
    {
        public ObjectId Id { get; set; }
        public DateTime Timestamp { get; set; }
    }

我需要创建一个mongo更新,如果文档中有任何标记不在提供的标记列表中,则删除子集合实体 .

它看起来像下面的更新应该工作,但当我测试它时,我在调用UpdateManyAsync时得到一个IndexOutOfRangeException .

System.IndexOutOfRangeException:索引超出了数组的范围 .

任何人都可以帮我弄清楚为什么这不起作用?请注意我使用的是C#.NET MongoDB驱动程序v2.0:

public async Task Test(ObjectId subEntityId, List<string> providedTags)
    {
        var subEntityIsInTheSubCollection = Builders<RootEntity>.Filter
            .ElemMatch(x => x.SubCollection, subEntity => subEntity.Id == subEntityId);

        var entityContainsTagsThatArentInProvidedList = Builders<RootEntity>.Filter
            .Where(root => root.Tags.Any(x => !providedTags.Contains(x)));

        var filter = Builders<RootEntity>.Filter.And(
            subEntityIsInTheSubCollection,
            entityContainsTagsThatArentInProvidedList);

        var update = Builders<RootEntity>.Update
            .PullFilter(x => x.SubCollection, subEntity => subEntity.Id == subEntityId);

        await _collection.UpdateManyAsync(filter, update);
    }

我已经创建了一个示例项目来重现这里可以找到的bug . https://github.com/nickmkk/UpdateWhereItemsArentInProvidedList

在RemoveSubEntityServiceTests.cs中运行测试以重现该问题 .

请注意,问题似乎与第25行的RemoveSubEntityService.cs中的过滤器有关