0

UPDATE: The return statement is still not working as expected to show single user detail by id in the DAO. I could only use for loop to iterate through the _id to match the userId, but when I click the edit button for the number of user will show all previous user Id in the console.

enter image description here

Another problem is when I call this method in the Service class, the output is null. Still crave for the solution to help me get over it.

@Override
public User get(Object userId) {

    User user = new User();
    
    FindIterable<Document> userTbl = database.getCollection("User").find();
for (Document doc : userTbl) {          

    String id = doc.getObjectId("_id").toString();
System.out.println("_id = " + id);

        if (id.equals(userId)) {
            return user;
        }
    }

    return null;
}

edit user in Service class

public void editUser() throws ServletException, IOException {

    Object userId = request.getParameter("id"); // get query string from the jsp

User user = userDAO.get(userId);


System.out.println("User full name is? " + user.getFullName());

}
Stu_Dent
  • 370
  • 1
  • 4
  • 19
  • 2
    There's so much wrong with that code I don't even know where to start. Overriding `userId` (a parameter!) shouldn't be done and on returning a new `User` object without populating it shouldn't be surprising that the name of that user is empty. I suggest you find out how to filter collections by using MongoDB filtering and not your own and then how to map the result of that filtering in a `User` object that returns the document you found and not an empty object. – Smutje Oct 12 '20 at 08:40
  • @Smutje sorry, you got any useful sources about it (MongoDB filtering)? I don't understand the document to Java... https://mongodb.github.io/mongo-java-driver/3.12/driver/tutorials/perform-read-operations/#query-filters – Stu_Dent Oct 12 '20 at 11:21
  • 1
    "For the find() method, you can also call the method without passing a filter object to match all documents in a collection." – Smutje Oct 13 '20 at 07:46
  • 1
    Does this answer your question? [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Smutje Oct 13 '20 at 07:48
  • @Smutje thanks for your reply. I got stuck on how to return the object id, I've tried using the compare it, but the return type can't let me compile. – Stu_Dent Oct 13 '20 at 07:51
  • @Smutje thanks for the hints, finally solved it! – Stu_Dent Oct 13 '20 at 11:32
  • 1
    The full name of the User returned by the DAO is null because the DAO returned an empty instance you instantiated within the body of the method rather than a `User` composed from the `Document` matching the ID. – vsfDawg Oct 13 '20 at 11:36

1 Answers1

0

After getting hints from @Smutje and think through it again, finally figured it out at my 2nd weeks of learning MongoDB. At my level I need to iterate the user document then find the id and return it.it

@Override
public User get(Object userId) {


FindIterable<User> userTbl = database.getCollection("User", User.class).find();
for (User doc : userTbl) {

    String id = doc.getId().toHexString();
    System.out.println("_id = " + id);

    if (id.equals(userId)) {
        return doc;
    }
}
return null;

}

Stu_Dent
  • 370
  • 1
  • 4
  • 19