2

I want to insert the following JSON into the Mongodb collection using java API . Here bookmarks is an arrayList of bookmarks POJO .

{
"_id": 5,
"email": "[email protected]",
"bookmarks": [
    {
        "name": "chipotle",
        "category": "restaurant",
        "stats": "203 likes",
        "tried": true

    },
    {
        "name": "olivegarden",
        "category": "restaurant",
        "stats": "203 likes",
        "tried": true

    }
]
}

I used the following API . but it doesn't seem to work

BasicDBObject document = new BasicDBObject();
document.append("email", userList.get(i).getEmail());   
document.append("bookmarks",   userList.get(i).getBookmarksList() ) ;   
WriteResult result = collection.insert(document);

This is the error I got when I ran the unit test .

java.lang.IllegalArgumentException: can't serialize class com.xxx.pojo.Bookmark
at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:272)
at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:173)
at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:119)
at com.mongodb.DefaultDBEncoder.writeObject(DefaultDBEncoder.java:27)
at com.mongodb.OutMessage.putObject(OutMessage.java:289)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:239)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:204)
at com.mongodb.DBCollection.insert(DBCollection.java:76)
at com.mongodb.DBCollection.insert(DBCollection.java:60)
at com.mongodb.DBCollection.insert(DBCollection.java:105)

Even after making the Bookmark POJO serializable I got the same error again .So I guess am using the Java API for insert in a wrong way . How to map the POJO directly into the mongodb element ?

1 Answer 1

1

As the error suggests can't serialize class com.xxx.pojo.Bookmark , which means that the List containing Bookmark.class can't be directly inserted in BasicDBObject document.

You need to use : BasicDBList as follows:

BasicDBList bookmark_list = new BasicDBList();
List<Bookmark> bmk_list = userList.get(i).getBookmarksList();
for(int i=0;i<bmk_list.size();i++)
{
String name = bmk_list.get(i).getName();
String category = bmk_list.get(i).getCategory();
String stats = bmk_list.get(i).getStats();
boolean tried = bmk_list.get(i).getTried();
DBObject db_obj = new BasicDBObject();
db_obj.put("name",name);
db_obj.put("category",category);
db_obj.put("stats",stats);
db_obj.put("tried",tried);
bookmark_list.add(db_obj);
}

Now add this bookmark_list in your document as follows:

BasicDBObject document = new BasicDBObject();
document.append("email", userList.get(i).getEmail());   
document.append("bookmarks",  bookmark_list ) ;  
WriteResult result = collection.insert(document);
Sign up to request clarification or add additional context in comments.

Comments

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.