Using the old MongoDB driver, I was able to perform the following query:
Query.Where("this.plan.sstats.used < this.plan.sstats.available")
But with the new one, I'm forced to write this:
builder.Filter
.Where(t => t.Plan.StorageStats.UploadUsed < t.Plan.StorageStats.UploadAvailable)
These look the same, but the new one does not work and I receive this error message:
Additional information: Unsupported filter: ({plan.sstats.used} < {plan.sstats.available}).
The back-end version is currently the same, so I don't see any reason why this shouldn't continue to be able to work.
How do I fix this? Is there a better way of doing this, whilst maintaining atomicity?
Seems MongoDb drive doesn't support it any more. Me personally see two possible solutions:
1) You query bson, not your objects, that should work (i have tried with my sample data out):
FilterDefinition<BsonDocument> filter =
new BsonDocument("$where", "this.plan.sstats.used<this.plan.sstats.available");
Waht is bad on this approach: your should query your collection as BsonDocument collection.
2) You query your collection as ToEnumerable() and than just add your filter as Where linq statement. That will work too, but you loose querying data directly on mongodb.
3) You could use aggregation framework, i did it this way:
var result = collection.Aggregate()
.Group(r => r.Plan.StorageStats.UploadUsed - r.Plan.StorageStats.UploadAvailable,
r => new {r.Key, Plans= r.Select(t=>t.Plan)} )
.Match(r=>r.Key < 0)
.ToEnumerable()
.SelectMany(r=>r.Plans);
Negative on aggregate is that you couldn't combine it with your other Filters you use in Find() call.
So, I'd also asked over on Mongo's JIRA and was given this as a potential alternative. I'm posting it here in case anyone is dissatisfied with Maksim's answer.
It's possible to create just a filter definition:
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\" }";