Milos Mijuskovic Milos Mijuskovic - 1 year ago 106
Java Question

How to insert a single field into a JSON using MongoTemplate?

So, i am making this program, trying to learn more about Spring and MongoDb. I have built this ticked module, that uses a MongoDb to store ticket info in a JSON format. It looks something like this:

> {
> "_id" : ObjectId("581fb1a24beb291d27f95a50"),
> "userID" : "581ddccb4beb29112a7b4f77",
> "ticketStatus" : "Processing",
> "ticketSolution" : "Not_Solved",
> "ticketComment" : null;
> }

My question is how do i insert a comment into the "ticketComment" field?
(I would need something that uses Criteria.where("ticketID").is(ticketID))

Answer Source

With simplest details use :-

Query query = new Query(criteria);
BasicDBObject newValues = new BasicDBObject(columnName,value);
BasicDBObject set = new BasicDBObject("$set", newValues);
Update update = new BasicUpdate(set);
mongoOperations.updateMulti(query, update, "collectionName")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download