gpt4 book ai didi

java - 使用吗啡数据存储在 mongodb 中查询和更新

转载 作者:行者123 更新时间:2023-11-29 05:41:23 27 4
gpt4 key购买 nike

我有如下类(class):

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

其中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""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

我做错了什么吗?

最佳答案

我不明白你为什么要查询 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());

关于java - 使用吗啡数据存储在 mongodb 中查询和更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17445916/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com