简体   繁体   中英

Mongo/Morphia querying for entire document

I run the following Mongo client command:

db.Role.insert(
    {
        name: "Common",
        alias: "COMMON",
        description: "Common role"
    }
);

This succeeds. Then inside my JVM app (that uses Mongo/Morphia 1.0.1 ) I run the following command:

System.out.println(datastore.find(Role.class)
    .field("alias").equal("COMMON").toString())

Which prints the following to the console:

{"alias": "COMMON"}

So it seems that my query is only querying my Role collection for a field with a key of " alias " and a value of " COMMON ". But I don't want this! I want a query that will return the entire " Common " Role document (as a JSON string, not mapped to a POJO). Any ideas?

Morphia's behavior is to return the full object. You need to explicitly ignore fields if you don't want to include them in your result.

Does your .toString() include all attributes?

Are you sure you're actually loading this document? Maybe there's another one with alias: "COMMON" as well?

You're missing the .get() (or .asList() if there were multiple ones):

System.out.println(datastore.find(Role.class)
    .field("alias").equal("COMMON").get().toString())

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