Skip to content

Commit ecae367

Browse files
committed
JAVA-2685: Deprecate CreateCollectionOptions autoIndex property
This property was deprecated in MongoDB 3.2 and removed in MongoDB 4.0. Along with deprecating the property, ensure that the field is only included in the "create" command if the property has explicitly been set to false. Otherwise, a 4.0 server will reject the command.
1 parent d8f4522 commit ecae367

File tree

5 files changed

+14
-2
lines changed

5 files changed

+14
-2
lines changed

driver-core/src/main/com/mongodb/client/model/CreateCollectionOptions.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ public class CreateCollectionOptions {
4242
* Gets if auto-index is enabled
4343
*
4444
* @return true if auto-index is enabled
45+
* @deprecated this option was deprecated in MongoDB 3.2 and removed in MongodB 4.0
4546
*/
47+
@Deprecated
4648
public boolean isAutoIndex() {
4749
return autoIndex;
4850
}
@@ -52,7 +54,9 @@ public boolean isAutoIndex() {
5254
*
5355
* @param autoIndex true if auto-index is enabled
5456
* @return this
57+
* @deprecated this option was deprecated in MongoDB 3.2 and removed in MongodB 4.0
5558
*/
59+
@Deprecated
5660
public CreateCollectionOptions autoIndex(final boolean autoIndex) {
5761
this.autoIndex = autoIndex;
5862
return this;

driver-core/src/main/com/mongodb/operation/CreateCollectionOperation.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import static com.mongodb.internal.async.ErrorHandlingResultCallback.errorHandlingCallback;
3737
import static com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol;
3838
import static com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocolAsync;
39+
import static com.mongodb.operation.DocumentHelper.putIfFalse;
3940
import static com.mongodb.operation.DocumentHelper.putIfNotZero;
4041
import static com.mongodb.operation.OperationHelper.LOGGER;
4142
import static com.mongodb.operation.OperationHelper.releasingCallback;
@@ -412,7 +413,7 @@ public void call(final AsyncConnection connection, final Throwable t) {
412413

413414
private BsonDocument getCommand(final ConnectionDescription description) {
414415
BsonDocument document = new BsonDocument("create", new BsonString(collectionName));
415-
document.put("autoIndexId", BsonBoolean.valueOf(autoIndex));
416+
putIfFalse(document, "autoIndexId", autoIndex);
416417
document.put("capped", BsonBoolean.valueOf(capped));
417418
if (capped) {
418419
putIfNotZero(document, "size", sizeInBytes);

driver-core/src/main/com/mongodb/operation/DocumentHelper.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ static void putIfTrue(final BsonDocument command, final String key, final boolea
3333
}
3434
}
3535

36+
static void putIfFalse(final BsonDocument command, final String key, final boolean condition) {
37+
if (!condition) {
38+
command.put(key, BsonBoolean.FALSE);
39+
}
40+
}
41+
3642
static void putIfNotNullOrEmpty(final BsonDocument command, final String key, final BsonDocument documentValue) {
3743
if (documentValue != null && !documentValue.isEmpty()) {
3844
command.put(key, documentValue);

driver-core/src/test/functional/com/mongodb/client/test/CollectionHelper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ public void create(final String collectionName, final CreateCollectionOptions op
127127
create(collectionName, options, WriteConcern.ACKNOWLEDGED);
128128
}
129129

130+
@SuppressWarnings("deprecation")
130131
public void create(final String collectionName, final CreateCollectionOptions options, final WriteConcern writeConcern) {
131132
drop(namespace, writeConcern);
132133
CreateCollectionOperation operation = new CreateCollectionOperation(namespace.getDatabaseName(), collectionName, writeConcern)

driver-core/src/test/functional/com/mongodb/operation/CreateCollectionOperationSpecification.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ class CreateCollectionOperationSpecification extends OperationFunctionalSpecific
146146
assert !collectionNameExists(getCollectionName())
147147
def operation = new CreateCollectionOperation(getDatabaseName(), getCollectionName())
148148
.capped(true)
149-
.autoIndex(false)
150149
.maxDocuments(100)
151150
.sizeInBytes(40 * 1024)
152151

@@ -172,6 +171,7 @@ class CreateCollectionOperationSpecification extends OperationFunctionalSpecific
172171
async << [true, false]
173172
}
174173

174+
@IgnoreIf({ serverVersionAtLeast(3, 7) })
175175
def 'should create collection in respect to the autoIndex option'() {
176176
given:
177177
assert !collectionNameExists(getCollectionName())

0 commit comments

Comments
 (0)