[CSHARP-1471] Find methods account for "IgnoreIfDefault" Created: 09/Nov/15  Updated: 31/Mar/22

Status: Backlog
Project: C# Driver
Component/s: Linq
Affects Version/s: 2.1
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Jeb Assignee: Unassigned
Resolution: Unresolved Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Minor Change

 Description   

Simple scenario here:

Trying to do a simple find on a field that is set to "IgnoreIfDefault", and thus is not a field in the document if the value is the default. However, this causes problems with querying...

Collection.Find(d => d.ActiveState == ActiveStateType.Active).ToListAsync().Result

With this class:

public class JebsClass {
     [BsonIgnoreIfDefault]
     [BsonDefaultValue(ActiveStateType.Active)]
     public ActiveStateType ActiveState { get; set; }
     public int Field1 {get; set; }
}

This finds records like this

{
     Field1: 5,
     ActiveState: "Active"
}

but NOT documents like this

{
     Field1: 5
}

It seems to me that the Find interface methods should take into account Default values, and update the translated query to include a $exists: false for ActiveState.



 Comments   
Comment by Matt [ 15/Jun/17 ]

This is causing issues for me in my project as well. I can no longer use LINQ expressions because of it, and have to fall back to the expression builder with Or(Exists(prop), Eq(prop, myDefaultValue)).

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