Try this:
ds.find(User.class).field("rights").sizeEq(0).get()
Question
i'm using MondoDB with Morphia 0.105 layer.
My User
class is:
@Entity
public class User {
@Id
private ObjectId id = null;
private String userId = null;
private String fullName = null;
@Embedded
private UserType userType = null;
@Embedded
private Set<Rights> rights = new HashSet<Rights>();
and my test class is:
public class Test {
public static void main(String[] args) throws UnknownHostException {
Morphia m = new Morphia();
Datastore ds = m.createDatastore(new MongoClient("localhost"), "test");
m.map(User.class);
User u = new User();
u.setFullName("User Name");
u.setUserId("USERID");
//u.getRights().add(Rights.ADMIN); //NO rights added VS ONE right added
ds.save(u);
u = ds.find(User.class).filter("rights size", 0).get();
System.out.println(u);
System.out.println(u != null);
}
}
I got this unexpected result:
The type(s) for the query/update may be inconsistent; using an instance of type 'java.lang.Integer' for the field 'org.vts.sis2.entities.User.rights' which is declared as 'java.util.Set'
If i uncomment line //u.getRights().add(Rights.ADMIN);
that adds to user a right, the query ds.find(User.class).filter("rights size", 1).get()
returns the correct result (even if the warning is displayed, but it's a false positive since in my opinion it's correct to compare a result of size operator to an integer)!
What should i do to query users with empty rights
list/set field?
Thanks
Solution
Try this:
ds.find(User.class).field("rights").sizeEq(0).get()