Duplicate Key Error from Bulk Inserts

981 views
Skip to first unread message

Jazz Pouls

unread,
Mar 11, 2015, 4:50:35 PM3/11/15
to mongod...@googlegroups.com
Hello,
I'm running MongoDB 2.6.3. (I had 3.0 but had to change due to versioning issue. I'm pretty sure I didn't get this error with that version, but I need to use this one.) So I'm trying to run this code in the Mongo shell in Terminal using the load(.js) command:
db = db.getSisterDB("myDatabase");

var bulk = db.privilege.initializeOrderedBulkOp();
bulk.insert( { _id: 1,  name: "a", key: "l" } );
bulk.insert( { _id: 2,  name: "b", key: "m" } );
bulk.insert( { _id: 3,  name: "c", key: "n" } );
bulk.insert( { _id: 4,  name: "d", key: "o" } );
bulk.insert( { _id: 5,  name: "e", key: "p" } );
bulk.insert( { _id: 6,  name: "f", key: "q" } );
bulk.insert( { _id: 7,  name: "g", key: "r" } );
bulk.insert( { _id: 8,  name: "h", key: "s" } );
bulk.insert( { _id: 9,  name: "i", key: "t" } );
bulk.insert( { _id: 10, name: "j", key: "u" } );
bulk.insert( { _id: 11, name: "k", key: "v" } );
bulk.execute();

It gives me this error:
2015-03-11T13:42:40.316-0700 BulkWriteError({
"writeErrors" : [
{
"index" : 0,
"code" : 11000,
"errmsg" : "E11000 duplicate key error index: myDatabase.privilege.$_id_ dup key: { : 1.0 }",
"op" : {
"_id" : 1,
"name" : "a",
"key" : "l"
}
}
],
"writeConcernErrors" : [ ],
"nInserted" : 0,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
}) at src/mongo/shell/bulk_api.js:1154

I've tried to dropIndexes() but it doesn't remove the _id index, because you can't remove that one. However, it seems like that's the index that has the duplicate. And it doesn't matter what value it is. I've tried something like 23873958237453 and it still says there is a duplicate key.
The error occurs at the bulk.execute() line.

Thanks for any insight you can provide.

Jazz Pouls

unread,
Mar 11, 2015, 5:59:13 PM3/11/15
to mongod...@googlegroups.com
UPDATE, so I've tried some things and adding "db.privilege" as the first argument of the insert() seemed to help (i.e. bulk.insert(db.privilege, {...}).
That at least delayed the error until the two lines after those that I showed you above.
Those two lines are :
db.privilege.ensureIndex( { name: 1 }, { unique: true } )
db.privilege.ensureIndex( { key: 1 },  { unique: true } )
Here the return values are:
> db.privilege.ensureIndex( { name: 1 }, { unique: true } )
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"errmsg" : "exception: E11000 duplicate key error index: octolytics.privilege.$name_1 dup key: { : null }",
"code" : 11000,
"ok" : 0
}
> db.privilege.ensureIndex( { key: 1 },  { unique: true } )
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"errmsg" : "exception: E11000 duplicate key error index: octolytics.privilege.$key_1 dup key: { : null }",
"code" : 11000,
"ok" : 0
}

So it's more duplicate key errors but its $name and $key instead of $_id_, like before.

Hopefully that helps explain my problem better.

Asya Kamsky

unread,
Mar 12, 2015, 1:16:11 AM3/12/15
to mongodb-user

What's in this collection?  If there is no data I'd say drop it and start from scratch...

Could you check what the output of db.system.namespaces.find() in that DB is?

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/55f3e4be-0aed-4ec1-94a2-7cdecca1e541%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jazz Pouls

unread,
Mar 12, 2015, 4:11:25 PM3/12/15
to mongod...@googlegroups.com
Thanks! that helped me figure this out. I'm a newbie with mongodb, so I had been trying to insert the same data in the same collections, without clearing them each time.  So once I used dropDatabase(), it worked. Thanks
Reply all
Reply to author
Forward
0 new messages