FindAndModify returning non updated value

I am using the following piece of code to update a token field in a collection -

  @Override
    public TestObject issueToken(long id, String objectId) {
        Query query = new Query();
        Criteria criteria = new Criteria("_id").is(new ObjectId(objectId));
        query.addCriteria(criteria);

        Update update = new Update();
        update.inc("lastIssuedTokenNo",1);

        FindAndModifyOptions options  = new FindAndModifyOptions();
        options.upsert(false);
        options.returnNew(true);
        Testobject tokenIssuedObject =  mongoTemplate.findAndModify(
                query,update,options,
                Testobject.class
        );
        if(ObjectUtils.isEmpty(tokenIssuedObject)) throw new ObjectRuntimeException(ErrorCodes.OBJECT_NOT_FOUND);
        return tokenIssuedObject;
    }

I have found few cases where this method is called and returned object doesn’t have the token value updated , what could be the possible reason for this?

Hi @Nishat_Kumar4 and welcome to the community forum!!

I tried to implement the below code in spring boot and t works well for me.

Service class:

public Document findAndModify(String objectId) {

       Document query = new Document("_id", new ObjectId(objectId));
       Document update = new Document("$inc", new Document("lastIssuedTokenNo", 1));
       Document options = new Document("returnDocument", "after");
       return collection.findOneAndUpdate( query, update);
   }

Main class:

@SpringBootApplication
public class AtlasSearchApplication {
	public static void main(String[] args) {
		String uri = "mongodb+srv://theuser:pwd@cluster0.k5dqp.mongodb.net/?retryWrites=true&w=majority&appName=cluster0";

		try (MongoClient mongoClient = MongoClients.create(uri)) {
			//MongoDatabase database = mongoClient.getDatabase("test");
			MovieAtlasSearchService searchService = new MovieAtlasSearchService(new MongoTemplate(mongoClient, "test"));
			Document findAndModify = searchService.findAndModify("6085c24f54b3444dd3fccc19");
			System.out.println("Result " + findAndModify);
		}}}

Could you help me with the error message that you are seeing while using the above code?

Regards
Aasawari

Hi Aaswari, thanks for your time. I’m not getting any errors as such, its just that at times even after this method is being called the token is not getting updated and we are receiving the object with the same tokenNo as it was before calling this method.

Can anybody help with this, its happening for 4 to 10 records daily, and based on my analysis its confirmed that the findAndUpdate method is returning the object without update. No concurrent call for same object is made so there is no chance for race condition. Happy to provide any other info required.

to check if the query is executed or not, I started updating lastUpdatedTime as well in the same query as shown below

@Override
public TestObject issueToken(long id, String objectId) {
Query query = new Query();
Criteria criteria = new Criteria(“_id”).is(new ObjectId(objectId));
query.addCriteria(criteria);

    Update update = new Update();
    update.inc("lastIssuedTokenNo",1);
    update.set("lastUpdatedTime", new Date());

    FindAndModifyOptions options  = new FindAndModifyOptions();
    options.upsert(false);
    options.returnNew(true);
    Testobject tokenIssuedObject =  mongoTemplate.findAndModify(
            query,update,options,
            Testobject.class
    );
    if(ObjectUtils.isEmpty(tokenIssuedObject)) throw new ObjectRuntimeException(ErrorCodes.OBJECT_NOT_FOUND);
    return tokenIssuedObject;
}

Post doing this I could see that lastUpdatedTime was getting updated to new value however lastIssuedTokenNo didn’t got updated. How is it possible for one field to get updated while other field not getting updated.