在mongodb集合中插入带有元素数组的文档的Java API

时间:2022-03-09 17:26:22

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

我希望使用java API将下面的JSON插入Mongodb集合。这里的书签是书签POJO的arrayList。

{
"_id": 5,
"email": "xxx@gmail.com",
"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

我使用了以下API。但这似乎行不通

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 ?

即使在做了书签POJO serializable之后,我还是犯了同样的错误。如何将POJO直接映射到mongodb元素?

1 个解决方案

#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.

如错误所示,不能序列化class com.xxx.pojo。书签,意思是包含书签的列表。类不能直接插入到BasicDBObject文档中。

You need to use : BasicDBList as follows:

您需要使用:BasicDBList如下:

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:

现在将这个bookmark_list添加到您的文档中,如下所示:

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

#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.

如错误所示,不能序列化class com.xxx.pojo。书签,意思是包含书签的列表。类不能直接插入到BasicDBObject文档中。

You need to use : BasicDBList as follows:

您需要使用:BasicDBList如下:

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:

现在将这个bookmark_list添加到您的文档中,如下所示:

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