javamongodbsortingdeprecatedmorphia

How to use Morphia to findAndModify with a sorted search query?


I am using Morphia 1.6.1 and this is my actual code:

final Query<Entity> query = datastore.createQuery(Entity.class)
    .field("status").equal(Status.OPEN)
    .order(Sort.ascending("lastUsed"));

final UpdateOperations<Entity> updateOperations = datastore
    .createUpdateOperations(Entity.class)
    .set("lastUsed", ZonedDateTime.now());

final Entity entity = datastore.findAndModify(query, updateOperations);

The only problem is that .order() is @Deprecated. So I need to find another way to do this. Do you have any ideas?


Solution

  • If you're on 1.6.1, you can happily use that method. Once you move to 2.x, the way that modify operations are done and how options are passed changes a bit so you'll need to do a bit of rework regardless. (There's an option to keep using the legacy API that's deprecated in it's entirety and will be removed soon. maybe in 2.4.)

    2.x is a complete rethink of that API in to what is intended to be a more reasonable, usable set of interfaces. So far folks seem happy with it. I'd highly recommend an upgrade.