10

I have a document as shown below

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

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

BiJ
  • 1,639
  • 5
  • 24
  • 55

1 Answers1

15

The following example removes the property activationToken from documents with the email xxx.xxx@xxx.com using the $unset update modifier:

Query query = new Query();
query.addCriteria(Criteria.where("email").is("xxx.xxx@xxx.com"));
Update update = new Update();
update.unset("activationToken");

// run update operation
mongoTemplate.updateMulti(query, update, User.class);
chridam
  • 100,957
  • 23
  • 236
  • 235