Now I want to query so as to get objects of"/>
  简体   繁体   中英

Querying and updating in mongodb using morphia datastore

I have classes as follows:

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

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

where, MoDBChannel class is:

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

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

and MoDBTopic class is:

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

and MoDBIA_DAO class:

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

I have an object of MoDBIAGlobals class saved as:

dataStore.save(globals)

where JSON of globals is as follows:

{"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>全局</ code>的JSON

Now I want to query so as to get objects of class MoDBIAGlobals where "id"=="usr1234" and "channelId"=="FB1234" . How can I create this query??

I tried like follows but I could not get any result:

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

it's printing result as:

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

Am I doing anything wrong?

I didn't understand why you're querying for data.channelId and data.data.topic if you want to find MoDBIAGlobals by id and channelId . And your data modeling it's a quite confusing too. Anyway, seems that your query does not match your document structure. The fields data.data.topic and data.channelId does not exists. Try to fix replacing with code bellow:

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

Now, if you want to query by id and channelId , try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM