When inserting a document using Collection.InsertMany that fails due to a unique index constraint the resulting mongo.InsertManyResults reports the documents ID as being Inserted.
It looks like that the results are actually computed before issuing the BulkWrite operation and never updated according to its outcome. Cleary, the results of the BulkWrite should be taken into account and only the IDs of successfully inserted documents may be returned together with the last error.
Here the result from the mentioned call:
&mongo.InsertManyResult{
InsertedIDs: {
primitive.ObjectID{0x5d, 0xab, 0x36, 0x86, 0xf, 0xa5, 0xbc, 0x20, 0x30, 0xd6, 0x1c, 0x8},
},
} mongo.BulkWriteException{
WriteConcernError: (*mongo.WriteConcernError)(nil),
WriteErrors: {
{
WriteError: mongo.WriteError{Index:0, Code:11000, Message:"E11000 duplicate key error collection: test.test index: _id_ dup key: { : ObjectId('5dab36860fa5bc2030d61c08') }"},
Request: nil,
},
},
}