Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring data mongodb remove a property from a document using MongoTemplate

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?

like image 585
BiJ Avatar asked Jan 01 '17 17:01

BiJ


1 Answers

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);
like image 139
chridam Avatar answered Oct 13 '22 00:10

chridam