Milos Mijuskovic Milos Mijuskovic - 22 days ago 8
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

With simplest details use :-

Criteria.where("ticketID").is(ticketID));
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")
Comments