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;
    }
ben_joseph
  • 1,660
  • 1
  • 19
  • 24
  • 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 – Neil Lunn Jun 09 '14 at 06:22
  • I have updated the question with my input JSON. – ben_joseph Jun 09 '14 at 06:26
  • How are you actually importing/inserting this? Presumably you have some code that is using the same value for the `_id` – Neil Lunn Jun 09 '14 at 06:31
  • I believe I'm not setting the id by myself. Doesn't mongodb create unique id by itself? – ben_joseph Jun 09 '14 at 06:33
  • 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. – Neil Lunn Jun 09 '14 at 06:36
  • Please see the code I just added. – ben_joseph Jun 09 '14 at 06:38
  • 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? – Neil Lunn Jun 09 '14 at 06:38
  • Is it because I'm not creating a new document object inside the create function? If yes, please add it as answer. – ben_joseph Jun 09 '14 at 06:41
  • 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. – Neil Lunn Jun 09 '14 at 06:45
  • Creating a new document object inside the create fucntion solved the problem. Sorry for my poor coding practices. @NeilLunn Post it as answer so that I can accept it. – ben_joseph Jun 09 '14 at 06:46

2 Answers2

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;
    }

...

    }
hcarrasko
  • 2,320
  • 6
  • 33
  • 45
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.

ben_joseph
  • 1,660
  • 1
  • 19
  • 24