vendredi 14 mars 2014

How can I query to find mongo entities whose list of sub-entities contain a field matching a string?


Vote count:

0




I have a collection of entities that look like this:



public class ClientEntity {

@Id
private String id;
@Indexed(unique = true)
private String clientId;
private String name;

@DBRef
private List<ClientMachineEntity> machines;

...
}


...where ClientMachineEntity looks like:



public class ClientMachineEntity {

@Id
private String id;
@Indexed(unique = true)
private String clientMachineId;
private String hostName;

...
}


I have a working search that finds ClientEntities by matching against "clientId" and "name":



public List<ClientEntity> searchByIdAndName(String id, String name) {

Criteria idCriteria = Criteria.where("clientId").regex(id, "i");
Criteria nameCriteria = Criteria.where("name").regex(name, "i");
Query query = new Query(new Criteria().orOperator(idCriteria, nameCriteria));
...
}


So my question is, how can I expand this search so that it also matches against "clientMachineId" in the list of sub-entities? I tried adding the following criteria:



Criteria machineCriteria = Criteria.where("machines.clientMachineId").regex(id, "i");


...but that doesn't work, presumably because machines is a LIST of entities, not just a single sub-entity.



asked 46 secs ago






Aucun commentaire:

Enregistrer un commentaire