How to query the latest inserted item in with Morphia And how to get the size of a collection?
It depends what you mean by latest, but if you have a Date field with the creation date then you would do this:
T latest = ds.find().sort("-dateCreated").get();
And this would get you the count:
int count = ds.getCount(Class.class);
When you save a document, the find method returns a object Key. If you want to know your object with the key that morphia insert in database, just get the key and put in the object parameter. I'm doing this way in my application.
To sort can you just pass .sort("dateCreated")
to sort in ASC, or .sort("-dateCreated")
that sort for the DSC way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With