[CSHARP-1467] Support of Inline Conditional-to-FilterDefinition Created: 02/Nov/15  Updated: 03/Dec/20  Resolved: 03/Dec/20

Status: Closed
Project: C# Driver
Component/s: API, Linq
Affects Version/s: 2.0.1
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Georgios Panagopoulos Assignee: Unassigned
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates CSHARP-1771 Support IIF method (i.e. ternary oper... Closed

 Description   

As a developer I want to be able to use iniline conditional statement in lamda expressions that generate filter definitions.

Example:
var posts = await blogContext.Posts.Find(p => (tag != null) ? p.Tags.Contains(tag) : true).ToListAsync();

currently in version 2.0.0.788 the above throws an error:
Unsupported filter: IIF(False, Serialization(Tags).Contains(null), True).



 Comments   
Comment by Jeffrey Yemin [ 03/Dec/20 ]

Closing as a duplicate of CSHARP-1771, which is also about support for IIF

Comment by Alex Zhuk [ 28/Jul/20 ]

Hello. As I can see now Mongodb supports https://docs.mongodb.com/manual/reference/operator/query/expr/#op._S_expr which allows to translate this query.

Can this be revisited?

Comment by Georgios Panagopoulos [ 02/Nov/15 ]

Hi Craig,

Thanks for getting back to me. I wouldn't expect it to be so difficult but it really depends on the internal implementation of the driver. Since you know the framework very well, I am sure that you are right. Feel free to reject the feature to keep the backlog clean. I can always try to implement it locally in a cloned repository and if I have any success then I can share the code and we can take it from there!

Thanks!

Comment by Craig Wilson [ 02/Nov/15 ]

Hi Georgios,

Thanks so much for the offer and the idea. Unfortunately, the MongoDB query language does not support this syntax in a query nor in a $match aggregation stage. It is supported, however, in a $project or $group stage and is already incorporated into the driver: http://mongodb.github.io/mongo-csharp-driver/2.1/reference/driver/expressions/#cond.

In theory, it would be possible to change this into a relatively complex $or statement: (tag != null && tags.Contains(tag)) || tag == null). This would, I believe, be rather difficult to automatically translate correctly in all cases when it would be simpler to require the user to provide us this expression themselves.

What are your thoughts?
Craig

Comment by Georgios Panagopoulos [ 02/Nov/15 ]

If accepted, please assign the issue to myself. I would like to become a contributor.

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