Search code examples
javaspringmongodbcriteriaspring-data-mongodb

MongoDB Spring data comparison between fields


I'm trying to simply do a comparison between my fields but it doesn't seems to work with spring data :

query.addCriteria(Criteria.where("active").gt("limit"));

Active and limit are 2 fields of my collection, and I wand to display all fields that exceed the limit. This limit is different for each item so I cannot do gt(200) for example...

There is anyway to do that ?


Solution

  • You can fall back to your java driver and issue a $where query:

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

    Anyway, you will have to issue a where command

    Also, take into account the Warning paragraph