Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-4066

Only use regex filters against string properties that are serialized as strings

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Major - P3 Major - P3
    • 2.15.0
    • 2.14.1
    • LINQ3
    • None
    • Fully Compatible

    Description

       

      I have a struct that has an "Id" field mapped using a custom serializer. Unfortunately LINQ queries, in my example Find, does not seem to use the serializer, causing the query to not match any documents.

      here is a full test case that reproduces the issue:

      class ObjectIdAsStringSerializer : SerializerBase<string>
      {
          public static readonly ObjectIdAsStringSerializer Instance = new ObjectIdAsStringSerializer();
       
          public override string Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
          {
              return context.Reader.ReadObjectId().ToString();
          }
       
          public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, string value)
          {
              context.Writer.WriteObjectId(ObjectId.Parse(value));
          }
      }
       
      struct Thing
      {
          public string Id { get; set; }
      }
       
      [Test]
      public async Task test_linq3()
      {
          BsonClassMap.RegisterClassMap<Thing>(cm =>
          {
              cm.MapIdProperty(thing => thing.Id)
                  .SetSerializer(ObjectIdAsStringSerializer.Instance);
          });
       
          MongoClientSettings settings = MongoClientSettings.FromConnectionString("mongodb://localhost:27017/?replicaSet=rs0");
          settings.LinqProvider = LinqProvider.V2; // V2 = test passes, V3 = test fails
          var client = new MongoClient(settings);
          string dbName = "testdb-" + Guid.NewGuid();
          IMongoDatabase database = client.GetDatabase(dbName);
          const string id = "590df61373b975210006fcdf";
          IMongoCollection<BsonDocument> coll = database.GetCollection<BsonDocument>("testCollection");
          await coll.InsertOneAsync(BsonDocument.Create(new Dictionary<string, object?>
          {
              ["_id"] = ObjectId.Parse(id)
          }));
          IMongoCollection<Thing> typedColl = database.GetCollection<Thing>("testCollection");
          long count = await typedColl.Find(t => t.Id == id).CountDocumentsAsync();
          await client.DropDatabaseAsync(dbName);
          Assert.That(count, Is.EqualTo(1));
      }
      

      Attachments

        Activity

          People

            robert@mongodb.com Robert Stam
            de.felix.koenig@gmail.com Felix König
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: