MongoDB / Morphia大小查询操作员故障?

时间:2013-12-10 15:48:06

标签: mongodb morphia

我正在使用带有Morphia 0.105图层的MondoDB。

我的User课程是:

@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>();

我的测试类是:

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);
    }

}

我得到了意想不到的结果:

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'

如果我取消注释向用户添加权限的行//u.getRights().add(Rights.ADMIN);,则查询ds.find(User.class).filter("rights size", 1).get()会返回正确的结果(即使显示警告,但这是误报,因为在我看来,这是正确的将size运算符的结果与整数进行比较)!

如何查询空rights列表/设置字段的用户?

由于

1 个答案:

答案 0 :(得分:1)

试试这个:

ds.find(User.class).field("rights").sizeEq(0).get()