Manju
Manju

Reputation: 103

How to update the document of Mongo DB without affecting one of its field value

I tried to find the answer for my above question, but failed to get it. Following is my work flow,

My JSP code is:

<f:form action="saveSubDepartment" modelAttribute="subDepartment" method="Post">

<f:hidden path="subDepartmentId"/>

<f:input path="subDepartmentName"/>

<f:input path="departmentId"/>

<!-- Many other fields-->

<input type="submit" name="create" value="Save">

</f:form>

My domain class in java is:

public class SubDepartment implements Serializable {

@Id
private String subDepartmentId=null;

private String departmentId;    
private String subDepartmentName;
private Date createdOn;
private Date modifiedOn;

/*corresponding setters and getters*/

}

I have a document in my DB as

{ 
"_id" : "17", 
"_class" : "somepackage.SubDepartment", 
"departmentId" : "DEPT1", 
"subDepartmentName" : "hello", 
"createdOn" : ISODate("2017-01-25T05:48:53.840+0000"), 
"modifiedOn" : ISODate("2017-01-25T07:19:15.616+0000"),
and so many....
}

When I click on save button, I want all the fields to be updated except the field createdOn, but when I use the following update query, createdOn field is setting to null, and my update method in DAO class is as follows

public String save(SubDepartment subDepartment){

String id=subDepartment.getSubDepartmentId();
subDepartment.setModifiedOn(new Date());
mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(id)), new Update(), SubDepartment.class);
mongoTemplate.save(subDepartment, subDepartmentCollection);

}

When I perform the above operation, createdOn field is setting to null since it is not mentioned anywhere in my JSP page as well.

So I tried to use

 <f:hidden path="createdOn">

but it did not work for me as it is a Date format at the backend.

Some how I made it to work by getting the field value(by executing one more query) before updating the whole document in my DAO class and setting it to the edited document.

Is there any other better solution/way for my requirement? Thanks in advance :-)

Upvotes: 0

Views: 1381

Answers (2)

Derrick
Derrick

Reputation: 1

In my understanding you try to update the modifiedOn. The code should be like this:

public String save(SubDepartment subDepartment){
   Query query = new Query(Criteria.where("_id").is(subDepartment.getSubDepartmentId()));
   Update update = Update.update("modifiedOn", new Date());
   mongoTemplate.updateFirst(query, update, SubDepartment.class);
}

Upvotes: 0

cralfaro
cralfaro

Reputation: 5948

I dont have access to the code right now, this afternoon i will update the response, but i think was soemthing like this.

public String save(SubDepartment subDepartment){

    String id=subDepartment.getSubDepartmentId();
    subDepartment.setModifiedOn(new Date());
    mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(id)), new Update("modifiedOn"), SubDepartment.class);
    mongoTemplate.save(subDepartment, subDepartmentCollection);

 }

In the new Update("modifiedOn"), you say all fields you want to update, if you don't say anything he tries to update all of them

Upvotes: 1

Related Questions