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

[BsonRepresentation(BsonType.String)]-attribute not respected in projection

    • Fully Compatible

      I have a class called Identity with two properties: IdentityType and IdentityNumber. The Type property is an enum with the [BsonRepresentation(BsonType.String)] attribute set, so it's stored as strings instead of integers.

      I now have a aggregation ending in a projection. In that projection, I have a $filter on an array of identities. That filter fails, and from what I can see, it fails because it tries to compare the stored string values to integer values.

      I'll try to give an representation of my data and types:

      Classes

      //root class for documents stored
      class Customer
        [BsonRepresentation(BsonType.ObjectId)]      
        string Id;
        User[] Users;
        Items[] Items;
      end;
      
      class User
        Identity Identity;
      end;
      
      class Identity
        [BsonRepresentation(BsonType.String)]
        IdentityType IdentityType;
        string IdentityNumber;
      end;
      
      class Item
        [BsonRepresentation(BsonType.ObjectId)]      
        string Id;
      end;
      

      My aggregation

      _collection.Aggregate()
        .Match(Builders<Customer>.Filter.In("Items.Id", ["id1", "id2"]))
        .Unwind<UnwindedCustomer>("Items")
        .Match(Builders<UnwindedCustomer>.Filter.In("Items.Id",  ["id1", "id2"]))
        .Project(doc => new
        {
          Id = doc.Items.Id,
          CustomerId = doc.Id,
          UserIsCustomer = doc.Users.Where(user =>
            user.Identity.IdentityNumber == "123" &&
            user.Identity.IdentityType == IdentityType.SomeType)
        });
      

      So I'm looking for some specific items, and for those items, I want to know which customer they belongs to, and if the given user is a listed user of that customer.

      If I render the last stage of the projection, I get something like

      { "$project" : 
        { 
          "Id" : "$Items.Id", 
          "CustomerId" : "$_id", 
          "UserIsCustomer" : 
          { "$filter" : 
              { 
                "input" : "$Users", 
                "as" : "user", 
                "cond" : { "$and" : [
                  { "$eq" : ["$$user.Identity.IdentityNumber", "123"] }, 
                  { "$eq" : ["$$user.Identity.IdentityType", 1] }
                ] } 
          } }, 
          "_id" : 0 
        } 
      }
      

      I would expect the projection to respect the BsonRepresentation attribute and render the last part of the condition as `

      { "$eq" : ["$$user.Identity.IdentityType", "SomeType"] }

      `

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            vegar@vi-kan.net Vegar Vikan
            Votes:
            2 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: