Question

I have an object which has 3 fileds:

public class tags{

@Property("n")
private String name;
@Property("t")
private int type;
@Property("r")
private int rank;

.....
}

I am using morphia to communicate to my MongoDB.

I want to save al lthe fileds to the DB, but while retreiving I want to query only based on the 'name' and 'type' fields within my object. I have tried using the @Transient Annotation, but it completely ignores the field during load/save.

Was it helpful?

Solution

This is a very common use case.

The morphia wiki describes using filters or fluent interface: https://github.com/mongodb/morphia/wiki/Query#wiki-filter

Here's an example:

ds.createQuery(tags.class).field('name').equal('idbentley').field('type').equal(1);

OTHER TIPS

If you are looking for limited query results https://github.com/mongodb/morphia/wiki/Query#wiki-ignoring-fields will cover that:

ds.createQuery(tags.class).retrievedFields(true, "name", "type").get();

Beware that you should only read these limited result sets or write back specific values. If you save them back, you will lose all the values you didn't retrieve.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top