3

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?

2
  • When you say old/new what exact version are you using? Commented Jan 11, 2017 at 9:45
  • @Liam New is 2.4, previous was 1.something. I'm not at work anymore so I can't check. Commented Jan 11, 2017 at 10:40

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Maksim, thanks for your reply. Unfortunately, we've been using it to do an atomic update, whilst ensuring that certain conditions are met, so this approach won't work.
@john you still could work directly with bson (solution 1) , otherwise you will have to split your query and first read all id's that you need to update and than build contains filter.
That's what I was hoping to avoid :) Thank you for your help.
2

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\" }";

1 Comment

cool, it works? you doing plain json filter and getting your Plans objects back?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.