Spring data mongodb remove a property from a document using MongoTemplate

9.3k views Asked by At

I have a document as shown below

{
    "_id" : ObjectId("5864ddd8e38112fd70b89893"),
    "_class" : "com.apic.models.UserReg",
    "name" : "xxx",
    "email" : "[email protected]"
    "activationToken" : "fe8376ea2dbdf61ebc"
}

How can I remove the property activationToken from it using Spring MongoTemplate?

1

There are 1 answers

0
chridam On BEST ANSWER

The following example removes the property activationToken from documents with the email [email protected] using the $unset update modifier:

Query query = new Query();
query.addCriteria(Criteria.where("email").is("[email protected]"));
Update update = new Update();
update.unset("activationToken");

// run update operation
mongoTemplate.updateMulti(query, update, User.class);