title: IBsonArraySerializer exception when querying using ElemMatch
body: <p>I have a new query for a collection that is already pulling and storing data for a configured type and I haven't come across that one before:</p>
  <pre><code>The serializer for field 'BookingRequests' must implement IBsonArraySerializer and provide item serialization info.
  </code></pre>
  <p>The object I have has the following structure:</p>
  <pre><code>public class DayBookings : IHasId
  {
      public DayBookings()
      {
          BookingRequests = new Dictionary&lt;string, TimestampedRequest&lt;BookingRequest&gt;&gt;();
      }
      public DayBookings(TimestampedRequest&lt;BookingRequest&gt; bookingRequest, string restaurant) : this()
      {
          Id = $&quot;{restaurant}_{bookingRequest.Request.Date.ToString(&quot;ddMMyyyy&quot;)}&quot;;
  
          Restaurant = restaurant;
  
          Date = bookingRequest.Request.Date;
  
          BookingRequests[bookingRequest.RequestId] = bookingRequest;
      }
      public string Id { get; set; }
      public string Restaurant { get; set; }
      public DateTime Date { get; set; }
      public Dictionary&lt;string, TimestampedRequest&lt;BookingRequest&gt;&gt; BookingRequests { get; set; }
  }
  </code></pre>
  <p>where <code>TimestampedRequest</code> is a standard POCO object with generic data inside.</p>
  <p>The configuration for that collection I run on start up is:</p>
  <pre><code>if (!BsonClassMap.IsClassMapRegistered(typeof(DayBookings)))
  {
      BsonClassMap.RegisterClassMap&lt;DayBookings&gt;(cm =&gt;
      {
          cm.AutoMap();
  
          cm.SetIdMember(cm.GetMemberMap(c =&gt; c.Id));
  
          var customDictionarySerializer = new DictionaryInterfaceImplementerSerializer&lt;Dictionary&lt;string, TimestampedRequest&lt;BookingRequest&gt;&gt;&gt;(dictionaryRepresentation: DictionaryRepresentation.Document,
                                                                                                                                                            keySerializer: new StringSerializer(BsonType.String),
                                                                                                                                                            valueSerializer: BsonSerializer.SerializerRegistry.GetSerializer&lt;TimestampedRequest&lt;BookingRequest&gt;&gt;());
        cm.GetMemberMap(c =&gt; c.BookingRequests)
          .SetSerializer(customDictionarySerializer);
     });
  
  }
  </code></pre>
  <p>I added the custom serializer for that field in case I needed it but it has had no effect.</p>
  <p>The query I'm trying to perform is this:</p>
  <pre><code>public async Task&lt;IEnumerable&lt;DateTime&gt;&gt; GetAllDatesWithUnconfirmedBookings(string restaurant)
          {
              IEnumerable&lt;DateTime&gt; dates = null;
  
              try
              {
                  dates = await Collection.Find(Builders&lt;DayBookings&gt;.Filter.And(
                                                  Builders&lt;DayBookings&gt;.Filter.Eq(d =&gt; d.Restaurant, restaurant),
                                                  Builders&lt;DayBookings&gt;.Filter.Gte(d =&gt; d.Date, DateTime.UtcNow.Date),
                                                  Builders&lt;DayBookings&gt;.Filter.ElemMatch(d =&gt; d.BookingRequests, b =&gt; b.Value.Request.Status == Domain.Requests.BookingStatus.Unconfirmed)))
                                          .SortBy(d =&gt; d.Date)
                                          .Project(d =&gt; d.Date)
                                          .ToListAsync();
              }
              catch (Exception ex)
              {
                  Logger.Error(ex, $&quot;Error getting all dates with pending bookings for Restaurant={restaurant}&quot;);
              }
  
              return dates;
          }
  </code></pre>
  <p>I couldn't find much information about the configuration I would have to set for the <code>BookingRequests</code> field so I would appreciate some help here.</p>
  <p>Many thanks!</p>
  <p><strong>UPDATE</strong></p>
  <p>I ended up changing the way of querying the DB and did not have to set up the serializer configuration. Suppressing ElemMatch and performing forEachAsync solved my problem:</p>
  <pre><code>public async Task&lt;IEnumerable&lt;DateTime&gt;&gt; GetAllDatesWithUnconfirmedBookingsAsync(string restaurant)
      {
          var dates = new List&lt;DateTime&gt;();
  
          try
          {
              await Collection.Find(Builders&lt;DayBookings&gt;.Filter.And(
                                      Builders&lt;DayBookings&gt;.Filter.Eq(d =&gt; d.Restaurant, restaurant),
                                      Builders&lt;DayBookings&gt;.Filter.Gte(d =&gt; d.Date, DateTime.UtcNow.Date)))
                              .SortBy(d =&gt; d.Date)
                              .ForEachAsync(d =&gt;
                              {
                                  if (d.BookingRequests.Values.Any(b =&gt; b.Request.Status == Domain.Requests.BookingStatus.Unconfirmed))
                                  {
                                      dates.Add(d.Date);
                                  }
                              });
          }
          catch (Exception ex)
          {
              Logger.Error(ex, $&quot;Error getting all dates with pending bookings for Restaurant={restaurant}&quot;);
          }
  
          return dates;
      }
  </code></pre>
  <p><strong>UPDATE: THIS MIGHT WORK</strong></p>
  <p>I have come across another Dictionary serialization problem with another problem and after looking for a while I found this answer: <a href="https://stackoverflow.com/a/57990191/5293466">https://stackoverflow.com/a/57990191/5293466</a> which has helped me getting the serialization issue go away. In fact, I got another query - not an ElemMatch but a PullFilter - working nicely against a <code>Dictionary&lt;string, CustomType&gt;</code>. I hope this helps people as it did it to me!</p>