6

What seems almost natural in simple SQL is impossible in mongodb.

Given a simple document:

{
    "total_units" : 100,
    "purchased_unit" : 60
}

I would like to query the collection, using spring data Criteria class, where "total_units > purchased_units".

To my understanding it should be as trivial as any other condition.

Found nothing to support this on Spring api.

2
  • I don't think Spring Data API supports this but you may need to wrap the $where query in your Java native DbObject
    – chridam
    Mar 26, 2015 at 13:42
  • @chridam, can you please add a simple example ? Mar 26, 2015 at 13:55

4 Answers 4

5

You can use the following pattern:

Criteria criteria = new Criteria() {
    @Override
    public DBObject getCriteriaObject() {
        DBObject obj = new BasicDBObject();
        obj.put("$where", "this.total_units > this.purchased_units");
        return obj;
    }
};

Query query = Query.query(criteria);
1
  • In my case the comparison of two fields worked like strings comparation. And this trick worked: this.total_units - this.purchased_units > 0. Also the getCriteriaObject can be shorter: return new BasicDBObject("$where", "this.total_units > this.purchased_units");
    – alaster
    Aug 21, 2017 at 16:26
4

I don't think Spring Data API supports this yet but you may need to wrap the $where query in your Java native DbObject. Note, your query performance will be fairly compromised since it evaluates Javascript code on every record so combine with indexed queries if you can.

Native Mongodb query:

db.collection.find({ "$where": "this.total_units > this.purchased_units" });

Native Java query:

DBObject obj = new BasicDBObject();
obj.put( "$where", "this.total_units > this.purchased_units");

Some considerations you have to look at when using $where:

Do not use global variables.

$where evaluates JavaScript and cannot take advantage of indexes. Therefore, query performance improves when you express your query using the standard MongoDB operators (e.g., $gt, $in). In general, you should use $where only when you can’t express your query using another operator. If you must use $where, try to include at least one other standard query operator to filter the result set. Using $where alone requires a table scan. Using normal non-$where query statements provides the following performance advantages:

MongoDB will evaluate non-$where components of query before $where statements. If the non-$where statements match no documents, MongoDB will not perform any query evaluation using $where. The non-$where query statements may use an index.

As far as I know you can't do query.addCriteria(Criteria.where("total_units").gt("purchased_units"));

but would go with your suggestion to create an additional computed field say computed_units that is the difference between total_units and purchased_units which you can then query as:

Query query = new Query();
query.addCriteria(Criteria.where("computed_units").gt(0));

mongoOperation.find(query, CustomClass.class);
4
  • 1
    thanks for you answer. However, these examples are not spring-data way. I have spent almost 2 days searching for solution but no luck. I guess I will need to add new field to this document to represent the value of "total_units - purchased_units" Mar 26, 2015 at 14:13
  • I would also take that route as an alternative
    – chridam
    Mar 26, 2015 at 14:26
  • 1
    @HananBareket Could you try the following: query.addCriteria(Criteria.where("this.total_units > this.purchased_units")?
    – chridam
    Mar 26, 2015 at 14:40
  • 4
    Already did. Doesn't work. I guess I am missing something. It doesn't make any sense that something that that is so fundamentally basic in any query language is not supported. Mar 29, 2015 at 6:30
1

Thanks @Andrew Onischenko for the historic good answer.

On more recent version of spring-data-mongodb (ex. 2.1.9.RELEASE), I had to write the same pattern like below:

import org.bson.Document;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

// (...)

Criteria criteria = new Criteria() {
    @Override
    public Document getCriteriaObject() {
        Document doc = new Document();
        doc.put("$where", "this.total_units > this.purchased_units");
        return doc;
    }
};

Query query = Query.query(criteria);
-1

One way is this:

Criteria c = Criteria.where("total_units").gt("$purchased_unit");
AggregationOperation matchOperation = Aggregation.match(c);
Aggregation aggregation = Aggregation.newAggregation(matchOperation);
mongoTemplate.aggregate(aggregation, "collectionNameInStringOnly", ReturnTypeEntity.class);

Remember to put collection name in string so as to match the spellings of fields mentioned in criteria with fields in database collection.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.