Skip to content

Commit 9421b50

Browse files
committed
improved ensureIndex to use default index name if null is provided, instead of getting mongoexception
1 parent 403d28d commit 9421b50

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/main/com/mongodb/DBCollection.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,14 +410,15 @@ public void ensureIndex( DBObject keys , String name )
410410
/**
411411
* Ensures an index on this collection (that is, the index will be created if it does not exist).
412412
* @param keys fields to use for index
413-
* @param name an identifier for the index
413+
* @param name an identifier for the index. If null or empty, the default name will be used.
414414
* @param unique if the index should be unique
415415
* @throws MongoException
416416
*/
417417
public void ensureIndex( DBObject keys , String name , boolean unique )
418418
throws MongoException {
419419
DBObject options = defaultOptions( keys );
420-
options.put( "name" , name );
420+
if (name != null && !name.isEmpty())
421+
options.put( "name" , name );
421422
if ( unique )
422423
options.put( "unique" , Boolean.TRUE );
423424
ensureIndex( keys , options );

0 commit comments

Comments
 (0)