Update a document from single person

I following this example to make a method for a single person to save email from input instead of fix value. Sadly it does not change anything at all. I wonder if I misunderstood the tutorial.

Here is the DAO class to hold the user object

public User update(User user) {

	MongoCollection<User> userTbl = database.getCollection("User", User.class);
	
	userTbl.updateOne(eq("_id", "id"),combine(set("email", user.getEmail())));

	return user;
}

Here is the Service to interact with the browser,

public void updateUser() throws ServletException, IOException {
	Object id = request.getParameter("userId");
String email = request.getParameter("email"); // attr is name = "email" from the input field
String fullName = request.getParameter("fullname");
String password = request.getParameter("password");

System.out.println("NEW ID = " + id + ", NEW EMAIL = " + email + ", NEW NAME = " + fullName
+ ", NEW PASSWORD =  " + password);

User user = new User(email, fullName, password);

userDAO.update(user);

}

Hello @Pat_Yue,

userTbl.updateOne(eq("_id", “id”),combine(set(“email”, user.getEmail())));

In this eq("_id", "id"), you should be using something like: eq("_id", user.getId()). Also, you don’t need to use the combine method as you are updating only one field, the email.

So, your update could be:

userTbl.updateOne(
    eq("_id", user.getId()), 
    set("email", user.getEmail())
);
1 Like

@Prasad_Saya Thanks for your reply. I’ve tried it, but still can’t update the email for a user in my return statement. Should I need to iterate the collection to update the document of single person, per said?

I think you missed to include the id field when constructing the user object in the updateUser() method. You can do this:

User user = new User(id, email, fullName, password);
// or
User user = new User(email, fullName, password);
user.setId(id)

@Prasad_Saya Thanks for your reply. I did and I’m in doubt with using this Object Id that I cast it to the constructor like below, or else I can’t pass the _id as it said:

The method setId(ObjectId) in the type User is not applicable for the arguments (Object)

If I could convert string to objectId could solve the problem.

I using POJOs by the way.

	public void updateUser() throws ServletException, IOException {

		Object id = (String) request.getParameter("id");
		String email = request.getParameter("email"); // attr is name = "email" from the input field
		String fullName = request.getParameter("fullname");
		String password = request.getParameter("password");
		
		System.out.println("NEW ID  = " + id + ", or NEW EMAIL = " + email + ", or NEW NAME = " + fullName
				+ ", NEW PASSWORD =  " + password);

		User user = new User((ObjectId) id, email, fullName, password);

		userDAO.update(user);

		String updateMsg = "User update done!";
		listUser(updateMsg);

	}

The User entity

public class User {

	private ObjectId id;

	@BsonProperty(value = "user_id")
	private String userId;
	private String email;
	private String fullName;
	private String password;

	public User() {
	}

	public User(ObjectId id, String email, String fullName, String password) {
		super();
		this.id = id;
		this.email = email;
		this.fullName = fullName;
		this.password = password;
	}
          //  getter and setter
}

Try using the ObjectId class to build it.

String idStr = (String) request.getParameter("id");
ObjectId objId = new ObjectId(idStr);

// ...

User user = new User(objId, email, fullName, password);
1 Like

@Prasad_Saya really thanks for your support. Somehow it has an exception with the ObjectId. I wonder if the ObjectId in the entity can use a simple type like string could retrieve the same result, which means that not using POJOs mapping at all? howvever, it will break everything of my other CRUD operation. :dizzy_face:

The server encountered an unexpected condition that prevented it from fulfilling the request.

java.lang.IllegalArgumentException org.bson.types.ObjectId.isValid(ObjectId.java:86) org.bson.types.ObjectId.parseHexString(ObjectId.java:528) org.bson.types.ObjectId.(ObjectId.java:205) com.smartcard.service.UserService.updateUser(UserService.java:97) com.smartcard.controller.admin.UpdateUserServlet.doPost(UpdateUserServlet.java:29) javax.servlet.http.HttpServlet.service(HttpServlet.java:652) javax.servlet.http.HttpServlet.service(HttpServlet.java:733) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

Please tell what is the value printed from the above System.out.println(....

1 Like

It shows the correct value, but the _id of the user in the console, e.g.
NEW ID = null, or NEW EMAIL = pepe@bbc.com, or NEW NAME = Pepe Ronaldo , NEW PASSWORD = pepe123

Here is the Servlets for update user I just call it from the Service class.

@WebServlet("/update_user")
public class UpdateUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");

		dBUtils.getMongoDB();

		UserService userService = new UserService(request, response);

		userService.updateUser();

	}
}

Sorry for the pain.

What is passed to the request?

1 Like

My apology!!! I made a typo to the JSP page and this is why I can’t get the parameter. I left userId instead of id while learning POJO few days ago (my bad)!
it now working like charm!
NEW ID = 5f8172544236e93fcf86c6e1, or NEW EMAIL = pepe@bbc.com, or NEW NAME = Pepe Ronaldo , NEW PASSWORD = pepe123

THANK YOU :heart: !!!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.