3

I have 2 columns String id, String Name and long age.

As we would have done in sql - select id FROM USERDETAILS Where Age > 5 would give me arrayList of primary keys that is "Id".

can anyone tell me how to do the same for mongo db using morphia.

I tried with query.retrieveFields(true,"id").get();

but it returns a USERDETAILS Model in return , i need only array of ids. how can i do this please help

Ekata
  • 229
  • 1
  • 6
  • 17
  • Check here http://stackoverflow.com/questions/3985214/mongodb-extract-only-the-selected-item-in-array – t4t Jul 08 '13 at 06:23
  • Using morphia, queries always return your model class. In your case, the name and age fields will be empty/null. So you can iterator over list and the the ids in your java app. – Miguel Cartagena Jul 08 '13 at 09:56
  • yep i have used iterative method but their were some performance issues. any way of avoiding iteration ? – Ekata Jul 08 '13 at 10:57
  • Well, you could use the java driver directly, but you have to transform a `DBCursor` in a `List` anyway. – Miguel Cartagena Jul 08 '13 at 11:43

2 Answers2

3

You're going to get back your model class with morphia. That's what it does. If you want the raw list of IDs, you'll need to use the driver directly.

evanchooly
  • 5,658
  • 1
  • 14
  • 22
0

use project

Query<Object> query = createQuery(); query.project("fieldNamewhichisalonerequired", true); return query.asList();