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 ?