Created
March 25, 2015 19:07
-
-
Save dmregister/757ab76234060e869b24 to your computer and use it in GitHub Desktop.
unique permalinks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Boolean agentSuccessfullyUpdated = false; | |
Integer attempt = 1; | |
while (agentSuccessfullyUpdated == false) { | |
try { | |
dbCollection.update(new BasicDBObject(agentMuw.getQuery()), new BasicDBObject(agentMuw.getModifiers()), agentMuw.isUpsert(), agentMuw.isMultiUpdate()); | |
agentSuccessfullyUpdated = true; | |
agentDbObjectForList.put("permalink", permalink); | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
//remove the old int from end of string (-1) | |
permalink = permalink.replaceAll("([-][\\d])+$", ""); | |
//create the new permalink with the incremented value | |
permalink = permalink+"-"+attempt; | |
//reset the permalink in the DBObject | |
agentSetOnInsert.put("permalink", permalink); | |
//update the permalink value | |
agentUpdate.put("$setOnInsert", agentSetOnInsert); | |
//reset the writable object | |
agentMuw = new MongoUpdateWritable(agentQuery, agentUpdate, true, false); | |
//increment the attempt | |
attempt++; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment