Search code examples
javamongodbmorphia

in morphia how to query and return a field based on two other integer fields are same or not


I have mongodb running and using morphia.
Having a Collection of BatchData Documents and need to filter out some field values.

This is the Entity:

@Entity
public class BatchData {

  @Id private ObjectId id;
  public int val1;
  public int val2;
  public String uuid;

}

If val1 equals val2 then the query should return uuid

This is as far as a get but ofcourse it does not work:

Query<BatchData> q = mongo.createQuery(BatchData.class).field("val1").equal("val2");
List<BatchData> entities = q.asList();

OR

Query<BatchData> q = mongo.createQuery(BatchData.class).field("val1").equal(BatchData.class.val2)

There can be one million or more BatchData Documents so i must only
return the uuid for performance reason.

Been reading the wiki and cannot understand or see a filter for two member fields
Morphia wiki


Solution

  • If the performance is very important, you should look at map-reduce function. Unfortunately, morphia does not support this MongoDB feature, so you need to work with java mongo driver itself. See example and docs.