java - Insert large JSON data from web service into mongodb -


i trying insert multiple records mongodb @ once return web service in json format.

but got following error :

    info: monitor thread connected server 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 work numeric keys, not: [_id]         @ org.bson.types.basicbsonlist._getint(basicbsonlist.java:168)         @ org.bson.types.basicbsonlist._getint(basicbsonlist.java:160)         @ org.bson.types.basicbsonlist.get(basicbsonlist.java:105)         @ com.mongodb.dbcollection.insert(dbcollection.java:309)         @ com.mongodb.dbcollection.insert(dbcollection.java:284)         @ com.mongodb.dbcollection.insert(dbcollection.java:250)         @ com.mongodb.dbcollection.insert(dbcollection.java:187)         @ santosh.gopharmatest.main(gopharmatest.java:80) 

i have used following code , please suggest result ?

        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                     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 can't used inserts of multiple documents, it's used arrays inside single document. bulk insert, need pass array of dbobjects insert method instead.

as per java doc insert() can accept either single dbobject or array or list of them.

so, in order save, need convert json array array/list of dbobjects, or save each array's item


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -