title: MongoDB UpdateMany Where One or More Array Items Aren't In Provided List
body: <p>I have a collection with documents that look like this:</p>
  
  <pre><code>{
      "_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&lt;string&gt; Tags { get; set; }
          public List&lt;SubEntity&gt; SubCollection { get; set; }
      }
  
      public class SubEntity
      {
          public ObjectId Id { get; set; }
          public DateTime Timestamp { get; set; }
      }
  </code></pre>
  
  <p>I need to create a mongo update that removes a subcollection entity if the document has any tags that aren't in a provided tag list.</p>
  
  <p>It looks to me like the update below should work but when I test it I get an IndexOutOfRangeException when calling UpdateManyAsync.</p>
  
  <blockquote>
    <p>System.IndexOutOfRangeException : Index was outside the bounds of the array.</p>
  </blockquote>
  
  <p>Can anyone help me figure out why this doesn't work?  Please note that I am using the C# .NET MongoDB Driver v2.0:</p>
  
  <pre><code>    public async Task Test(ObjectId subEntityId, List&lt;string&gt; providedTags)
      {
          var subEntityIsInTheSubCollection = Builders&lt;RootEntity&gt;.Filter
              .ElemMatch(x =&gt; x.SubCollection, subEntity =&gt; subEntity.Id == subEntityId);
  
          var entityContainsTagsThatArentInProvidedList = Builders&lt;RootEntity&gt;.Filter
              .Where(root =&gt; root.Tags.Any(x =&gt; !providedTags.Contains(x)));
  
          var filter = Builders&lt;RootEntity&gt;.Filter.And(
              subEntityIsInTheSubCollection,
              entityContainsTagsThatArentInProvidedList);
  
          var update = Builders&lt;RootEntity&gt;.Update
              .PullFilter(x =&gt; x.SubCollection, subEntity =&gt; subEntity.Id == subEntityId);
  
          await _collection.UpdateManyAsync(filter, update);
      }
  </code></pre>
  
  <p>I've created a sample project to reproduce the bug which can be found here.
  <a href="https://github.com/nickmkk/UpdateWhereItemsArentInProvidedList" rel="nofollow">https://github.com/nickmkk/UpdateWhereItemsArentInProvidedList</a></p>
  
  <p>Run the tests in RemoveSubEntityServiceTests.cs to reproduce the issue.</p>
  
  <p>Note that the problem seems to be with the filter in RemoveSubEntityService.cs on line 25</p>