It's me
It's me

Reputation: 1115

Storing a values with ArrayList<Model> to MongoDB in Java

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?

Upvotes: 1

Views: 10930

Answers (2)

Ori Dar
Ori Dar

Reputation: 19000

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

Upvotes: 5

arunb2w
arunb2w

Reputation: 1196

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

Upvotes: 0

Related Questions