0

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.

Pi Horse
  • 2,350
  • 8
  • 30
  • 51
  • So you want to save certain properties but not load them? – evanchooly Feb 24 '14 at 20:29
  • I want to save them all and retrieve all of them too. But when I query based on the object, I want to query just based on the 'name' and 'rank' fields. – Pi Horse Feb 24 '14 at 20:36

2 Answers2

2

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);
idbentley
  • 4,188
  • 3
  • 34
  • 50
0

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.

xeraa
  • 10,456
  • 3
  • 33
  • 66