java - 将来自 Web 服务的大型 JSON 数据插入 mongodb

标签 java json mongodb

我正在尝试一次将多条记录插入 MongoDB,这些记录由 Web 服务以 JSON 格式返回。

但我收到以下错误:

    INFO: Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 0, 4]}, minWireVersion=0, maxWireVersion=3, electionId=null, maxDocumentSize=16777216, roundTripTimeNanos=545196}
    Exception in thread "main" java.lang.IllegalArgumentException: BasicBSONList can only work with numeric keys, not: [_id]
        at org.bson.types.BasicBSONList._getInt(BasicBSONList.java:168)
        at org.bson.types.BasicBSONList._getInt(BasicBSONList.java:160)
        at org.bson.types.BasicBSONList.get(BasicBSONList.java:105)
        at com.mongodb.DBCollection.insert(DBCollection.java:309)
        at com.mongodb.DBCollection.insert(DBCollection.java:284)
        at com.mongodb.DBCollection.insert(DBCollection.java:250)
        at com.mongodb.DBCollection.insert(DBCollection.java:187)
        at santosh.GoPharmaTest.main(GoPharmaTest.java:80)

我使用了以下代码,请建议我该怎么做才能得到结果?

        package santosh;

        import java.io.BufferedReader;
        import java.io.IOException;
        import java.io.InputStreamReader;
        import java.net.HttpURLConnection;
        import java.net.URL;

        import com.mongodb.DB;
        import com.mongodb.DBCollection;
        import com.mongodb.DBCursor;
        import com.mongodb.DBObject;
        import com.mongodb.MongoClient;
        import com.mongodb.util.JSON;

        public class GoPharmaTest {

            @SuppressWarnings("deprecation")
            public static void main(String[] args) {
                String response="";
                MongoClient mongoclient=new MongoClient("localhost", 27017);
                DB db = mongoclient.getDB("admin");
                DBCollection collection = db.getCollection("product");

                String url = "user web service";

                URL obj;
                try {

                    obj = new URL(url);
                    HttpURLConnection con = (HttpURLConnection) obj.openConnection();

                    // optional default is GET
                    con.setRequestMethod("GET");

                    //add request header
                    //con.setRequestProperty("User-Agent", USER_AGENT);

                    //int responseCode = con.getResponseCode();
                    //StringBuffer response = new StringBuffer();

                    BufferedReader in = new BufferedReader(
                            new InputStreamReader(con.getInputStream()));
                    String inputLine;

                    while ((inputLine = in.readLine()) != null) {
                        response+=inputLine;
                    }
                    in.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                DBObject dbObject = (DBObject)JSON.parse(response);
                collection.insert(dbObject);

                DBCursor cursorDoc = collection.find();
                while (cursorDoc.hasNext()) {
                    System.out.println(cursorDoc.next());
                }
                System.out.println("Done");
            }
        }

最佳答案

BasicDBList 不能用于插入多个文档,它仅用于单个文档内的数组。要执行批量插入,您需要将 DBObject 数组传递到插入方法中。

根据java doc insert() 可以接受单个 DBObject 或它们的数组或列表。

因此,为了保存,您需要将 JSON 数组转换为数组/DBObjects 列表,或者保存每个数组的项目

关于java - 将来自 Web 服务的大型 JSON 数据插入 mongodb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31653349/

相关文章:

javascript - 未处理的 promise 拒绝: TypeError: Cannot read property 'username' of undefined

javascript - contenteditable 的用途是什么?我可以将其用作编辑个人资料按钮吗

javascript - 尝试使用 Request NPM 将数据从 NodeJS 发布到本地主机(Loopback Swagger API)

java - Action 监听器java问题

java - 使用接口(interface)将方法应用于 ArrayList

java - 解析 Booking.uz.gov.ua 网站

json - 从 Postgres 中的 JSON 数组中选择

java - Couchbase 在 Java 中批量检索多个 key

java - Eclipse 假设错误的编码并更改了所有项目文件的编码

json - Powershell 解析 Swagger Json