I want to have common method for inserting and getting objects from MongoDB collection. For all mongo db operations I am using Jongo library. Here's my code:
public UserModel getUserByEmailId(String emailId) {
String query = "{emailId:'"+emailId+"'}";
Object obj = storage.get(query);
UserModel user = (UserModel) obj;
//getting exception on above line. I am sure that I have UserModel
//type of data in obj
// Exception is: java.lang.ClassCastException: Cannot cast java.util.LinkedHashMap to UserModel
return user;
}
Here is "storage.get(String query)" method. My intention is to have common method to read data from mongo db. That's why I want it to return Object. (Feel free comment if I'm wrong)
public Object get(String query) {
Object obj = collection.findOne(query).as(Object.class);
return obj;
}
//Here: collection is my "org.Jongo.MongoCollection" type object.
What is the right way to get UserModel type of object from "Object"? Let me know if you need more information