문제

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.

도움이 되었습니까?

해결책

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top