1

My query look like this:

var query = from p in collection
where p.MinStockQuantity >= p.StockQuantity
select p;

I can't run because I have exception: Unsupported filter: ([MinStockQuantity] >= [StockQuantity])

This query also does not work, the same bug.

    var collection = database.GetCollection<Product>("Product");
    var builder = Builders<Product>.Filter;
    var filter = builder.Where(o => o.MinStockQuantity > o.StockQuantity);
    var query = collection.Find(filter).ToListAsync().Result;

How can I compare 2 fields ?

2 Answers 2

1

I know I am quite late but this works perfectly,try this :

  var collection = database.GetCollection<Product>("Product");
        var builder = Builders<Product>.Filter;
        var filter = builder.Gt(o => o.MinStockQuantity , o.StockQuantity);
        var query = collection.Find(filter).ToListAsync().Result;

Gt here is greater than. there are various other methods like Gte =greater than equal to etc .

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

Comments

0

So my previous answer was obviously wrong. You shoud use that query

db.products.find({ $where: "this.MinStockQuantity > this.StockQuantity" })

To run this query in c# world you need to use BSON documents:

var doc = MongoDB.Bson.Serialization.BsonSerializer
         .Deserialize<BsonDocument>
         ("{$where: \"this.MinStockQuantity > this.StockQuantity\"}");
var result = collection.Find(new CommandDocument(doc));

I used that query in my test application and it yells proper results.

9 Comments

Thanks, but I have the same bug. MongoDB does not allow for comparison between two columns from each other?
@user5472920 what are the types of columns? Mongo allows you to compare columns from same object
@user5472920 could you post here whole exception with stacktrace? Maybe there is something in inner exception
I have bad news, MongoDB don;t support this queries :( groups.google.com/forum/#!topic/mongodb-user/bWmQ02jGi5A These queries are not supported because LINQ and FilterDefinitionBuilder queries need to be translated into the underlying MongoDB query language, and MongoDB queries don't support comparing two fields in a document to each other.
@user5472920 I have even worse news, it works just fine :) Check my edited answer
|

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.