Error 11000 in Mongo Java, DuplicateKeyException

217 views Asked by At

I have a piece of code, as following and have been getting this error in Java. coll is the collection where I need to insert data. I was hoping mongodb would automatically generate the ids so putting the same data (in a for loop) wouldn't matter but I get that error. What should I do?

     //   String json1 = XML.toJSONObject(xml_text).toString();
     //   DBObject dbo = (DBObject) com.mongodb.util.JSON.parse(json1);
     //   List<DBObject> list = new ArrayList<>();
     //   list.add(dbo);

        for (int i = 0; i < 5000; i++) {
            coll.insert(list);
        }

db.collection.getIndexes returns

switched to db ice
> db.next.getIndexes()
[
    {
            "v" : 2,
            "key" : {
                    "_id" : 1
            },
            "name" : "_id_",
            "ns" : "ice.next"
    }
]
>
1

There are 1 answers

0
Shagufta Oliveyu Methwani On BEST ANSWER

Resolved. I was using the same DBObject in the loop, hence the error. Creating one inside the loop now, no issues. Thanks everyone.