0

I get this error while trying to insert data from a JSON file. Only one item is added to the database.

11000 E11000 duplicate key error index: awdb.mycollection.$_id_  dup
> key: { : ObjectId('53954d897aadbe032a33cd68') }

> > db.mycollection.getIndexes()
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "awdb.mycollection"
        },
        {
                "v" : 1,
                "unique" : true,
                "key" : {
                        "UDID" : 1
                },
                "name" : "UDID_1",
                "ns" : "awdb.mycollection",
                "sparse" : true
        }
]
>

The following is the JSON input file I am using.

{"UDID":"1234","FriendlyName":"Ben Android","sn":"abc123","ManDate":"12/12/8234"}
{"UDID":"1235","FriendlyName":"Android","sn":"abc23","ManDate":"12/12/1254"}
{"UDID":"1236","FriendlyName":"Ben droid","sn":"abc12","ManDate":"12/12/1734"}

Here is the code im using to insert the JSON

while ((sCurrentLine = br.readLine()) != null) {
d=g.fromJson(sCurrentLine, Device.class);
m.create(d);
}

And here is my create function

public boolean create(Device d) {
        document.put("UDID",d.UDID);
        document.put("name", d.FriendlyName);
        document.put("Serial", d.sn);
        document.put("Manf", d.ManDate);
        collection.insert(document);
        return true;
    }

And my Device class

    public class Device {

public  String UDID;
public  String FriendlyName;
public  String sn;
public  String ManDate;
    }
10
  • The _id value is the primary key, which does not allow duplicates. It appears your JSON has the same value mentioned at least twice. You need to de-duplicate your JSON or otherwise resolve the problem Commented Jun 9, 2014 at 6:22
  • I have updated the question with my input JSON. Commented Jun 9, 2014 at 6:26
  • 1
    I believe we cannot see any code here to determine any different. As it stands there is not enough information for anyone else to solve your problem. Commented Jun 9, 2014 at 6:36
  • 1
    For example where is document being instantiated? Is there a new instance per loop? Or is it created outside the loop and you are just replacing the values? See the possible problems here? Commented Jun 9, 2014 at 6:38
  • 1
    Very possible that it seems to contain a singular _id value that is not being overwritten. That generates on the client by default. But what I am saying is there not the relevant code posted here for anyone to tell for sure. It certainly seems that way by the error being produced. It would be clearer if we could see the code. Commented Jun 9, 2014 at 6:45

2 Answers 2

1

You need do this:

public boolean create(Device d) {
        BasicDBObject document = new BasicDBObject();
        document.put("UDID",d.UDID);
        document.put("name", d.FriendlyName);
        document.put("Serial", d.sn);
        document.put("Manf", d.ManDate);
        collection.insert(document);
        return true;
}

And then you will create a new object to insert in you collection. Hope this helps.

EDIT: Also is a good practice use getters and setters in you code like this:

        public class Device {

        private  String UDID;
        private  String FriendlyName;
        private  String sn;
        private  String ManDate;

    public String getUDID(){
    return this.UDID;
    }
    public String getFriendlyName(){
    return this.FriendlyName;
    }
    public void setUDID(String UDID){
    return UDID = this.UDID;
    }
    public String setManDate(String ManDate){
    return ManDate = this.ManDate;
    }

...

    }
Sign up to request clarification or add additional context in comments.

Comments

0

The problem was that, as @NeilLunn pointed out, I was not creating a new BasicDBObject each time I inserted a row. This meant that, it was trying to insert a row with the same ObjectId with only the rest of the values changed. This was causing the above mentioned error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.