Skip to content

Instantly share code, notes, and snippets.

@gscheibel
Created April 11, 2012 15:37
Show Gist options
  • Save gscheibel/2360083 to your computer and use it in GitHub Desktop.
Save gscheibel/2360083 to your computer and use it in GitHub Desktop.
nextValue
DBCollection currentCollection = this.getCollection( key );
DBCursor cursor = currentCollection.find().sort( new BasicDBObject( ID_FIELDNAME, -1 ) ).limit( 1 );
if ( cursor.hasNext() ) {
DBObject maxObject = cursor.next();
Object idField = maxObject.get( ID_FIELDNAME );
if ( idField.getClass().equals( Integer.class ) || idField.getClass().equals( Long.class ) ) {
long maxId = (Long) idField;
value.initialize( maxId + increment );
}
else {
log.unableToIncrementValue( currentCollection.getName(), idField.getClass().getName() );
}
}
else {
value.initialize( initialValue );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment