我的课程如下:
@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}}}
现在我想查询以获取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());
我做错了吗?
答案 0 :(得分:3)
如果您想要data.channelId
和data.data.topic
找到MoDBIAGlobals
,我不明白为什么要查询id
和channelId
。而且你的数据建模也很混乱。无论如何,您的查询似乎与您的文档结构不匹配。字段data.data.topic
和data.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());
现在,如果您想按id
和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());