Spring data mongodb remove a property from a document using MongoTemplate
Asked Answered
H

1

10

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?

Hoptoad answered 1/1, 2017 at 17:57 Comment(0)
E
17

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);
Exstipulate answered 1/1, 2017 at 21:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.