Search code examples
mongodbmorphia

Morphia - HasAnyOf


The HasAnyOf feature of Morphia does not seem to be working or Im doing something wrong.

The data is saved correctly on DB. Im using Morphia 1.0.1 with MongoDB/Drive 3.0.1

Below my class mapping and query:

//freight
@Entity(value="freight", noClassnameStored = true)
public class Freight extends BaseEntity {
@Embedded
protected List<Vehicle> vehicles;

//vehicle
@Entity(value="vehicle", noClassnameStored = true)
public class Vehicle extends BaseEntity{...

//Query
Query<Freight> query = getDatastore().createQuery(Freight.class);
    query.disableValidation();

if(vo.getVehicles() != null && !vo.getVehicles().isEmpty()){
        query.field("vehicles").hasAnyOf(vo.getVehicles());     
    }       

    return query.asList();

Any suggestion is welcome.

Thanks!


Solution

  • This should work — we are using .hasAnyOf() successfully with the same versions.

    You should probably change the vehicle entity to

    //vehicle
    @Embedded
    public class Vehicle {...
    

    Since you are embedding it, it's not a (standalone) entity. I'm not sure what BaseEntity does, but if it adds an ObjectId, timestamps,... — that's not necessary for embedded entities, since all the information is already in freight.