[CSHARP-2106] Projection fails when expression has field names that are substrings of each other Created: 20/Nov/17  Updated: 14/Jun/19  Resolved: 18/Mar/19

Status: Closed
Project: C# Driver
Component/s: API, Command Operations
Affects Version/s: 2.4.4
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Vadim Rybak Assignee: Wan Bachtiar
Resolution: Cannot Reproduce Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows .NetStandard 2.0


Issue Links:
Duplicate
is duplicated by CSHARP-2097 ProjectionDefinition.Expression does ... Closed

 Description   

When you have a nested object where there are 2 properties such that the name of the first property starts with the name of the second property.

public class SubscriberSource
{
/// ...
public DateTime? StatusChangeDate
{ get; set; }
public SubscriberStatus Status { get; set; }
}

And you try to build a projection, it will not generate anything for the projection at all. There is probably somewhere in the code that a .startsWith is utilized to add fields to the projection builder for some reason.
So when i write something like

_collection.Find(query).Project(x => new
{ x.StatusChangeDate, x.Status }
)

No projection is passed to mongoDb at all and all fields are returned.



 Comments   
Comment by Wan Bachtiar [ 22/Feb/19 ]

No projection is passed to mongoDb at all and all fields are returned.

Hi Vadim,

I tried to reproduce this issue with both MongoDB .NET/C# v2.4.4 and also the current stable v2.7.3 without success.

Since you didn't provide the mapping for SubscriberStatus, I'm using the following mapping example:

    public class SubscriberSource
    {
        public ObjectId Id { get; set; }     
        public DateTime? StatusChangeDate { get; set; }
        public SubscriberStatus Status {get; set; }
        public String Baz {get; set; }
    }
    public class SubscriberStatus
    {
        public String Foo {get; set; }
        public int Bar {get; set;}
    }

Using the following example:

    var query = Builders<SubscriberSource>.Filter.Eq(x => x.Baz, "x");
    var results = collection.Find(query).Project(x => new {x.StatusChangeDate, x.Status}).FirstOrDefault();

The result projects field StatusChangeDate and all fields nested under Status. You can also project only specific field(s) nested under Status by specifying it. The example below projecting field StatusChangeDate and only field Foo nested under Status:

    var results = collection.Find(query).Project(x => new {x.StatusChangeDate, x.Status.Foo}).FirstOrDefault();

If you still have further question, could you please provide:

  • Mapping definition of SubscriberStatus
  • The output projected fields that you're getting
  • The output projected fields that you're expecting to see (which field you don't want to be returned)

Regards,
Wan.

Comment by Vadim Rybak [ 29/Nov/18 ]

This seems like a pretty serious issue, is there any kind of ETA on this? I am surprised that more people are not being effected by this.

Generated at Wed Feb 07 21:41:37 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.