Exception in thread \"main\" com.mongodb.MongoException$DuplicateKey: { \"serverUsed\" : \"localhost/127.0.0.1:27017\" , \"err\" : \"E11000 duplicate key
Try calling myIdMapCollection.save(myObj); instead of myIdMapCollection.insert(myObj);
The save method, unlike insert does upsert, meaning if a document contains _id, it replaces that document.
My guess is that you had fetched the DBObject using a cursor | query, had manipulated it, and you want to persist the changes. In that case, save is the right way to do it.
So, when calling insert the DBObject is already associated with _id, calling insert thus fails, because you already have a document with that _id in the collection, which should be unique (duplicate index error).