BasicBSONList can only work with numeric keys, not: [_id]

mongodb

Solution

BasicDBList can't be used to do inserts of multiple documents, it's only used for arrays inside a single document. To do a bulk insert, you need to pass an array of DBObjects into the insert method instead.

I changed your code to do this, and it worked without error:

    StringBuffer sb = new StringBuffer();
    int valuecount = 0;
    final QuoteReportBean[] quotelist = {new QuoteReportBean()};
    DBObject[] totalrecords = new BasicDBObject[quotelist.length];
    for (int i = 0; i < quotelist.length; i++) {
        QuoteReportBean reportbean = quotelist[i];
        valuecount++;
        BasicDBObject dbrecord = new BasicDBObject();
        dbrecord.append("cust_id", reportbean.getCustomerId());
        dbrecord.append("unique_symbol", reportbean.getUniqueSymbol());
        sb.append(reportbean.getUniqueSymbol() + ",");
        dbrecord.append("exch", reportbean.getExchange());
        dbrecord.append("access_time", reportbean.getDate());
        totalrecords[i] = dbrecord;
    }
    WriteResult result = coll.insert(totalrecords, WriteConcern.NORMAL);

Problem

I am trying to insert multiple records to MongoDB at once , so for this i created a javaBean for each record to be inserted and added them to ArrayList . And finally from the ArrayList , i am trying to perform a insert operation as shown below ``` public void insert(ArrayList<QuoteReportBean> quotelist) { BasicDBList totalrecords = new BasicDBList(); StringBuffer sb = new StringBuffer(); int valuecount=0; for (QuoteReportBean reportbean: quotelist) { valuecount++; BasicDBObject dbrecord = new BasicDBObject(); dbrecord.append("cust_id", reportbean.getCustomerId()); dbrecord.append("unique_symbol", reportbean.getUniqueSymbol()); sb.append(reportbean.getUniqueSymbol()+","); dbrecord.append("exch", reportbean.getExchange()); dbrecord.append("access_time", reportbean.getDate()); totalrecords.add(dbrecord); } WriteResult result = coll.insert(totalrecords,WriteConcern.NORMAL); } ``` But i am the follwoing error ``` Exception in thread "taskExecutor-1" java.lang.IllegalArgumentException: BasicBSONList can only work with numeric keys, not: [_id] at org.bson.types.BasicBSONList._getInt(BasicBSONList.java:159) at org.bson.types.BasicBSONList._getInt(BasicBSONList.java:150) at org.bson.types.BasicBSONList.get(BasicBSONList.java:104) at com.mongodb.DBCollection.apply(DBCollection.java:501) at com.mongodb.DBCollection.apply(DBCollection.java:491) at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:195) at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:180) at com.mongodb.DBCollection.insert(DBCollection.java:58) ``` Could anybody please help me as how to resolve this ??

Original source