0

I'm trying to migrate to Mongo Morphia 2. Documentation is very poor. How should I rewrite the following function

return datastore.get(type, id.toUUID());

I've tried this

return datastore.find(type).field("_id_").equal(id.toUUID()).first();

I'm not sure how get(T, String) is mapped. I cannot find documentation on that function. I explored the data on MongoDB, I saw that all indexes have an "id" field.

nicholas
  • 413
  • 1
  • 4
  • 7

1 Answers1

1

You'd write something like: datastore.find(type).filter(eq("_id", id.toUUID())).first(). You can also just refer to the name of the java field and Morphia will map that correctly.

What version are you updating from? That API looks crazy old to me.

evanchooly
  • 6,102
  • 1
  • 16
  • 23
  • I thought datastore.get(type, id.toUUID()); the ID refers to the default ID field Mongo uses. Which I thought is "\_id\_" – nicholas Nov 11 '21 at 14:02
  • 1
    `_id_` is the index name. `_id` is the document field name. the only time you refer to index names is when giving a `hint` about which index to use. Always refer to either the document names or java field names when writing queries. – evanchooly Nov 11 '21 at 14:28
  • Thank you. This was helpful. Please include it in your answer. – nicholas Nov 12 '21 at 11:56
  • Any time! Happy to help. – evanchooly Nov 12 '21 at 11:59