1

我有如下课程:

@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}}}

对象 <code>globals</code> 的 JSON

现在我想查询以获取MoDBIAGlobalswhere"id"=="usr1234""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());

它的打印结果为:

results: { "data.channelId" : "FB1234" , "data.data.topic" : "NO_TOPIC"}
results.count: 0

我做错什么了吗?

4

1 回答 1

3

我不明白您为什么要查询data.channelId以及data.data.topic是否要MoDBIAGlobals通过idand查找channelId。而且您的数据建模也很混乱。无论如何,您的查询似乎与您的文档结构不匹配。该字段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());

现在,如果你想通过idand查询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("id").equal("user1234");

QueryResults<MoDBIAGlobals> results = dao.find(query);
System.out.println("results: " + results);
System.out.println("results.count: " + results.countAll());
于 2013-07-03T12:52:31.280 回答