3

I am currently in the process of refactoring some of our mongo querying code to get rid of methods deprecated in Morphia 1.3. I am running into an issue when trying to run a find and Modify query with an index . At the moment our legacy code is setting an index on the query in this fashion: ds.createQuery(Entity.class).hintIndex("index")

I have removed it to be in line with morphia 1.3 and the code now looks something like this : new FindOptions.modifier("$hint", "index"); return query.asList(findOptions);

As a result we do not set the index on the query until actually running it. This is fine of get queries, less so for upsert ones. To replace the deprecated findAndModify : findAndModify(Query<T> query, UpdateOperations<T> operations, boolean oldVersion, boolean createIfMissing), morphia is asking to use findAndModify(Query, UpdateOperations, FindAndModifyOptions)

In this case, I would have expected to have a modifier method in the FindAndModifyOptions object to be able to set an index, when running the query. I can't seem to be able to do something like this:

FindAndModifyOptions findOptions = new FindOptions();
findOptions.modifier("$hint", SUGGESTION_CONTENT_TYPE_INDEX_NAME);
return datastore.findAndModify(query, updateOperations, findAndModifyOptions);

Can someone advise on how to pass the index? Is it possible to do it via the FindAndModifyOptions object ?if not, what would be the best way to set the index for the upsert query

0 Answers0