Skip to content

Instantly share code, notes, and snippets.

@dmregister
Created March 25, 2015 19:07
Show Gist options
  • Save dmregister/757ab76234060e869b24 to your computer and use it in GitHub Desktop.
Save dmregister/757ab76234060e869b24 to your computer and use it in GitHub Desktop.
unique permalinks
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