1

In my Java Play framework application, I want to store the ArrayList values in mongoDB.

I am having 3 values in the JSON which is loginid, phone, students; In that students is the ArrayList. I am storing the data in mongoDB like this:

{  "loginid" : "[email protected]", "phone" : "0123456789", "students" : [{"firstName" : "Jesse", "lastName" : "Varnell", "age" : "15", "gender" : "M" }, { "firstName" : "John", "lastName" : "Doe", "age" : "13", "gender" : "F"}] }

I am using mongo query to store the values like:

BasicDBObject searchQuery = new BasicDBObject();
BasicDBObject theUserObj = new BasicDBObject();

ArrayList<Student> student = new ArrayList<Student>();
if(studentArray != null && studentArray.size()>=0) {
    Student stud = new Student();
    for(int i = 0; i < studentArray.size(); i++){
        stud = studentArray.get(i);
        student.add(stud);
    }   
}
theUserObj.put("loginid", profile.loginid);
theUserObj.put("phone", profile.phone);
searchQuery.append("loginid", username);
theUserObj.put("students", student);

theUserCollection.update(searchQuery, theUserObj); //Got error on this line.

I am getting the following error:

Execution exception (In /app/controllers/StudentInfo.java around line 176)
IllegalArgumentException occured : can't serialize class models.Student

play.exceptions.JavaExecutionException: can't serialize class models.Student
    at play.mvc.ActionInvoker.invoke(ActionInvoker.java:237)
    at Invocation.HTTP Request(Play!)
Caused by: java.lang.IllegalArgumentException: can't serialize class models.Student
    at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:234)
    at org.bson.BasicBSONEncoder.putIterable(BasicBSONEncoder.java:259)
    at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:198)
    at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:140)
    at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:86)
    at com.mongodb.DefaultDBEncoder.writeObject(DefaultDBEncoder.java:27)
    at com.mongodb.OutMessage.putObject(OutMessage.java:142)
    at com.mongodb.DBApiLayer$MyCollection.update(DBApiLayer.java:346)
    at com.mongodb.DBCollection.update(DBCollection.java:165)
    at com.mongodb.DBCollection.update(DBCollection.java:197)
    at com.mongodb.DBCollection.update(DBCollection.java:209)
    at controllers.StudentInfo.doStoreProfile(StudentInfo.java:176)
    at controllers.StudentInfo.storeUserProfile(StudentInfo.java:212)
    at play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:557)
    at play.mvc.ActionInvoker.invoke(ActionInvoker.java:508)
    at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:484)
    at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:479)
    at play.mvc.ActionInvoker.invoke(ActionInvoker.java:161)

How to store a value with ArrayList in MongoDB using Java?

5
  • Does your Student class implement Serializable ? Commented Jan 30, 2014 at 9:51
  • No. It is one of the Model in Play framework. Commented Jan 30, 2014 at 9:54
  • Can you edit this class ? Try to make it implement Serializable/ Commented Jan 30, 2014 at 9:55
  • Yeah sure. Let me try that now. Commented Jan 30, 2014 at 9:55
  • Tried, Still same error. Commented Jan 30, 2014 at 10:00

2 Answers 2

5

DBObject and implementing classes can only handle BSON types, which you Student class is not.

There are lots of framework which can map | convert domain objects (such as Student) to | from BSON representation and documents. Since you don't use any apparently, you will have to manually convert each Student to a DBObject yourself.

BasicDBObject searchQuery = new BasicDBObject("loginid", username);
BasicDBObject theUserObj = new BasicDBObject();

List<Object> studentsDBList = new BasicDBList();

for (Student student : studentArray) {
    DBObject studentDBObject = new BasicDBObject();
    studentDBObject.put("firstName", student.getFirstName());
    studentDBObject.put("lastName", student.getLastName());
    studentDBObject.put("age", student.getAge());
    studentDBObject.put("gender", student.getGender());
    studentsDBList.add(studentDBObject);
} 

theUserObj.put("phone", profile.phone);
theUserObj.put("students", studentsDBList);

theUserCollection.update(searchQuery, theUserObj); 

Also, note that you don't have to put loginid into theUserObj, as you are only querying for it

Sign up to request clarification or add additional context in comments.

2 Comments

Hello Ori, How can you do the reverse of this? getting MongoDb values as an arraylist
Following the above example : List<Object> aList = (List<Object>) theUserObj.get("students"). The get method returns a BasicDBList which inherits from ArrayList
0

You are trying to add a normal JAVA object into MongoDB.

ArrayList<Student> student = new ArrayList<Student>();
theUserObj.put("students", student);

In the above stmt, u are trying to put student ArrayList object into MongoDB arrays. Thus why Mongo throws serializable error.

You should initialize your array using DBObject, and try to use them in you code to avoid this error.

List<DBObject> students= new ArrayList<>();

And try to add the student details in this object.

Or u can also use BasicDBList as below

for(Tag tag:tags){
    BasicDBList student= new BasicDBList();
     for(int i = 0; i < studentArray.size(); i++){
    stud = studentArray.get(i);
    student.add(stud);
}

The above may contain some syntax errors but those type of initializing should help to add array of values inside Mongo through JAVA

2 Comments

stud = studentArray.get(i); student.add(stud); this wouldn't work as Stundent objects are still added to a List
Yes..the codew may contains some errors.I did not execute and check.But am trying to imply that you cannot insert JAVA object into Mongo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.