[CSHARP-1889] Where clause comparison failure Created: 11/Jan/17  Updated: 27/Oct/23  Resolved: 11/Jan/17

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

Type: Task Priority: Minor - P4
Reporter: John Fricker Assignee: Robert Stam
Resolution: Works as Designed Votes: 0
Labels: question
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

I'm trying to update our mongo driver from an older version to the 2.4 version, but I've hit a problem.

Old code:

Query.Where("this.plan.sstats.used < this.plan.sstats.available")

New code:

builder.Filter.Where(t => t.Plan.StorageStats.UploadUsed < t.Plan.StorageStats.UploadAvailable)

Using the new code returns the following error, whereas the old code worked:

Additional information: Unsupported filter: ({plan.sstats.used} < {plan.sstats.available}).

Is there anything I can do to resolve this and retain the atomic nature of the old operation?



 Comments   
Comment by Robert Stam [ 11/Jan/17 ]

The 2.x filter builders do not have a helper method for creating "$where" filters. I imagine this is on purpose because we want to discourage use of $where.

But if you still want to use "$where" in the 2.x API you can use a JsonFilterDefinition and pass it the "$where" expression directly like this:

FilterDefinition<C> filter = new JsonFilterDefinition<C>("{ $where : \"this.plan.sstats.used < this.plan.sstats.available\" }");

Since strings can be converted to FilterDefinitions you could also write either of the following which both end up creating a JsonFilterDefinition:

var filter = (FilterDefinition<C>)"{ $where : \"this.plan.sstats.used < this.plan.sstats.available\" }";
 
// or using an implicit conversion
 
FilterDefinition<C> filter = "{ $where : \"this.plan.sstats.used < this.plan.sstats.available\" }";

Comment by Robert Stam [ 11/Jan/17 ]

Those are two different types of Where.

The legacy API Query.Where translated into a "$where" query operator. "$where" is very expensive and normally not recommended.

The second Where is a type-safe way of creating a filter to be used with Find and the error indicates that the given expression cannot be translated to the MongoDB query language (the reason is that the MongoDB query language only supports comparing a field to a constant, not comparing two fields).

I'll have to investigate if there is a way to use "$where" with the 2.x filter builders.

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