Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve a specific field from an entity using Morphia and Play framework in Java

I have a big entity I created in Play! framework in Java and I would like to retrieve only a specific field from the database using Morphia and MongoDB. The entity itself is very big and contains many fields, so I would like to improve the performance of my application by retrieving only one field from it.

I know it is possible but I can't figure out exactly how to do it...
Here are some of the fields I have in my class Shop:

public String imagePath;
public String profileImagePath;
public String Motto;
@Reference
public Category primeCategory;
public boolean isOnline;

As you can see, the primeCategory field is a Category entity which I want to retrieve. I saw something which suppose to achieve that in Morphia's website:

Datastore ds = null; 

Shop shop = ds.createQuery(Shop.class).retrievedFields(true, "primeCategory").get();

I'm not sure what exactly do I'm getting here.

It would be great if someone could explain to me if this is the way and how exactly I should do it, as well as provide an example.

like image 337
elad Avatar asked Oct 21 '12 07:10

elad


1 Answers

try this

Shop.createQuery().retrievedFields(true, "primeCategory").get()
like image 60
fxp Avatar answered Oct 31 '22 17:10

fxp