Home Ask Login Register

Developers Planet

Your answer is one click away!

Death-Stalker February 2016

MongoDB generating same ID between inserts

I am using pymongo and I am trying to insert dicts into mongodb database. My dictionaries look like this

{
    "name" : "abc",
    "Jobs" : [
        {
            "position" : "Systems Engineer (Data Analyst)",
            "time" : [
                "October 2014",
                "May 2015"
            ],
            "is_current" : 1,
            "location" : "xyz",
            "organization" : "xyz"
        },
        {
            "position" : "Systems Engineer (MDM Support Lead)",
            "time" : [
                "January 2014",
                "October 2014"
            ],
            "is_current" : 1,
            "location" : "xxx",
            "organization" : "xxx"
        },
        {
            "position" : "Asst. Systems Engineer (ETL Support Executive)",
            "time" : [
                "May 2012",
                "December 2013"
            ],
            "is_current" : 1,
            "location" : "zzz",
            "organization" : "xzx"
        },
    ],
    "location" : "Buffalo, New York",
    "education" : [
        {
            "school" : "State University of New York at Buffalo - School of Management",
            "major" : "Management Information Systems, General",
            "degree" : "Master of Science (MS), "
        },
        {
            "school" : "Rajiv Gandhi Prodyogiki Vishwavidyalaya",
            "major" : "Electrical and Electronics Engineering",
            "degree" : "Bachelor of Engineering (B.E.), "
        }
    ],
    "id" : "abc123",
    "profile_link" : "example.com",
    "html_source" : "<html> some_source_code </html>"
}

I am getting this error:

pymongo.errors.DuplicateKeyError: E11000 duplicate key error index: Linkedin_DB.employee_info.$id dup key: { : ObjectId('56b64f6071c54604f02510a8') }

When I run my program 1st document gets inserted properly but when I insert the second document I get this er

Answers


Bernie Hackett February 2016

The problem is that your save method is using a field called "id" to decide if it should do an insert or an upsert. You want to use "_id" instead. You can read about the _id field and index here. PyMongo automatically adds an _id to you document if one is not already present. You can read more about that here.

Post Status

Asked in February 2016
Viewed 3,025 times
Voted 7
Answered 1 times

Search




Leave an answer


Quote of the day: live life