使用morphia数据存储区在mongodb中查询和更新

时间:2013-07-03 10:44:45

标签: java mongodb mongodb-query morphia

我的课程如下:

@Entity("Collection_IAGlobals")
public class MoDBIAGlobals {
    @Id
    @Indexed
    String id;

    @Embedded
    Map<EnumChannelType, MoDBChannel> data = new HashMap<EnumChannelType, MoDBChannel>();
}

其中,MoDBChannel类是:

@Entity("Collection_IAGlobals")
@Embedded
public class MoDBChannel extends MoDBTopic {
    @Indexed
    private String channelId;

    @Embedded
    private Map<String, MoDBTopic> data = new HashMap<String, MoDBTopic>();
}

和MoDBTopic类是:

@Entity("Collection_IAGlobals")
@Embedded
public class MoDBTopic {
    private String topic;
    private Double score1 = 0.0;
    private Double score2 = 0.0;
}

和MoDBIA_DAO类:

public class MoDBIA_DAO extends BasicDAO<MoDBIAGlobals, String> {
    public MoDBIA_DAO(Mongo mongo, Morphia morphia, String dbName) {
        super(mongo, morphia, dbName);
    }
}

我有一个MoDBIAGlobals类的对象保存为:

dataStore.save(globals)

其中全局变量的JSON如下:

{"id":"usr1234",
"data":{"FACEBOOK":{"channelId":"FB1234",
                    "data":{"NO_TOPIC":{"topic":"NO_TOPIC",
                                        "score1":1.0,
                                        "score2":0.0}},
                    "score1":0.0,
                    "score2":0.0}}}

JSON of object <code>globals</code>

现在我想查询以获取MoDBIAGlobals"id"=="usr1234"的{​​{1}}类对象。我该如何创建这个查询??

我尝试了以下但我无法得到任何结果:

"channelId"=="FB1234"

打印结果如下:

MoDBIA_DAO dao = new MoDBIA_DAO(mongo, morphia, DB_Name);
Datastore dataStore = morphia.createDatastore(mongo, DB_Name);
Query<MoDBIAGlobals> query = dataStore.createQuery(MoDBIAGlobals.class).disableValidation();

query.field("data.channelId").equal("FB1234");
query.field("data.data.topic").equal("NO_TOPIC");
QueryResults<MoDBIAGlobals> results = dao.find(query);
System.out.println("results: " + results);
System.out.println("results.count: " + results.countAll());

我做错了吗?

1 个答案:

答案 0 :(得分:3)

如果您想要data.channelIddata.data.topic找到MoDBIAGlobals,我不明白为什么要查询idchannelId。而且你的数据建模也很混乱。无论如何,您的查询似乎与您的文档结构不匹配。字段data.data.topicdata.channelId不存在。尝试使用以下代码修复替换:

MoDBIA_DAO dao = new MoDBIA_DAO(mongo, morphia, DB_Name);
Datastore dataStore = morphia.createDatastore(mongo, DB_Name);
Query<MoDBIAGlobals> query = dataStore.createQuery(MoDBIAGlobals.class).disableValidation();

query.field("data.FACEBOOK.channelId").equal("FB1234");
query.field("data.FACEBOOK.data.topic.NO_TOPIC").equal("NO_TOPIC");
QueryResults<MoDBIAGlobals> results = dao.find(query);
System.out.println("results: " + results);
System.out.println("results.count: " + results.countAll());

现在,如果您想按idchannelId进行查询,请尝试以下操作:

MoDBIA_DAO dao = new MoDBIA_DAO(mongo, morphia, DB_Name);
Datastore dataStore = morphia.createDatastore(mongo, DB_Name);
Query<MoDBIAGlobals> query = dataStore.createQuery(MoDBIAGlobals.class).disableValidation();

query.field("data.FACEBOOK.channelId").equal("FB1234");
query.field("id").equal("user1234");

QueryResults<MoDBIAGlobals> results = dao.find(query);
System.out.println("results: " + results);
System.out.println("results.count: " + results.countAll());