Reputation: 81
Based on id I have to update a field in mongodb database.I am using spring-mvc,angularjs and mongodb. Id is comming to my spring class but its not updating. I am giving one sample of data which is in mongodb. From that data i have to update status field.
//mongodb
{ "_id" : ObjectId("556459bc4f3c9aac453f711d"),
"name" : "TestTask3",
"type" : null,
"status" : "Pending Approval",
"detail" : "TEst",
"comments" : "TEst"
}
//Spring class
public void approveTask(String id){
try {
DBCollection collection = mongoTemplate.getCollection(RelmanUtil.MONGO_DB_COLL_TASKS);
BasicDBObject query = new BasicDBObject("_id",id);
BasicDBObject update = new BasicDBObject();
update.put("$set", new BasicDBObject("status", "Assigned"));
collection.update(query, update);
} catch (Exception e) {
e.printStackTrace();
}
}
Upvotes: 0
Views: 63
Reputation: 7840
You missed thing that you passed id
as string to mongo ObjectId
check this you should changed your code as below :
import org.bson.types.ObjectId;
public void approveTask(String id) {
try {
DBCollection collection = mongoTemplate.getCollection(RelmanUtil.MONGO_DB_COLL_TASKS);
BasicDBObject query = new BasicDBObject("_id", ObjectId(id));
//OR BasicDBObject query = new BasicDBObject("_id", new ObjectId(id));
BasicDBObject update = new BasicDBObject();
update.put("$set", new BasicDBObject("status", "Assigned"));
collection.update(query, update);
} catch(Exception e) {
e.printStackTrace();
}
}
put id
in ObjectId(id) or new ObjectId(id)
Upvotes: 2