Skip to content

Commit 5e60d12

Browse files
committed
CSHARP-1413: Add support for IndexOptionDefaults to CreateCollection (2.x Legacy API).
1 parent c8d2e34 commit 5e60d12

File tree

5 files changed

+88
-7
lines changed

5 files changed

+88
-7
lines changed

src/MongoDB.Driver.Core.Tests/Core/Operations/CreateCollectionOperationTests.cs

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,28 @@ public void Execute_should_create_collection_when_Capped_is_set(
390390
}
391391
}
392392

393+
[Test]
394+
[RequiresServer("DropCollection", MinimumVersion = "3.2.0-rc0")]
395+
public void Execute_should_create_collection_when_IndexOptionDefaults_is_set(
396+
[Values(false, true)] bool async)
397+
{
398+
var storageEngineOptions = new BsonDocument("mmapv1", new BsonDocument());
399+
var indexOptionDefaults = new BsonDocument("storageEngine", storageEngineOptions);
400+
var subject = new CreateCollectionOperation(_collectionNamespace, _messageEncoderSettings)
401+
{
402+
IndexOptionDefaults = indexOptionDefaults
403+
};
404+
405+
using (var binding = CoreTestConfiguration.GetReadWriteBinding())
406+
{
407+
var result = ExecuteOperation(subject, binding, async);
408+
409+
result["ok"].ToBoolean().Should().BeTrue();
410+
var collectionInfo = GetCollectionInfo(binding, _collectionNamespace.CollectionName);
411+
Assert.That(collectionInfo["options"]["indexOptionDefaults"], Is.EqualTo(indexOptionDefaults));
412+
}
413+
}
414+
393415
[Test]
394416
[RequiresServer("DropCollection")]
395417
public void Execute_should_create_collection_when_MaxDocuments_is_set(
@@ -485,13 +507,7 @@ public void Execute_should_create_collection_when_Validator_is_set(
485507
var result = ExecuteOperation(subject, binding, async);
486508

487509
result["ok"].ToBoolean().Should().BeTrue();
488-
var commandOperation = new ReadCommandOperation<BsonDocument>(
489-
_collectionNamespace.DatabaseNamespace,
490-
new BsonDocument("listCollections", 1),
491-
BsonDocumentSerializer.Instance,
492-
new MessageEncoderSettings());
493-
var commandResult = commandOperation.Execute(binding, CancellationToken.None);
494-
var collectionInfo = commandResult["cursor"]["firstBatch"].AsBsonArray.Where(c => c["name"] == _collectionNamespace.CollectionName).Single();
510+
var collectionInfo = GetCollectionInfo(binding, _collectionNamespace.CollectionName);
495511
Assert.That(collectionInfo["options"]["validator"], Is.EqualTo(new BsonDocument("_id", new BsonDocument("$exists", true))));
496512
Assert.That(collectionInfo["options"]["validationAction"].AsString, Is.EqualTo("error"));
497513
Assert.That(collectionInfo["options"]["validationLevel"].AsString, Is.EqualTo("strict"));
@@ -626,6 +642,17 @@ private void DropCollection()
626642
}
627643
}
628644

645+
private BsonDocument GetCollectionInfo(IReadBinding binding, string collectionName)
646+
{
647+
var commandOperation = new ReadCommandOperation<BsonDocument>(
648+
_collectionNamespace.DatabaseNamespace,
649+
new BsonDocument("listCollections", 1),
650+
BsonDocumentSerializer.Instance,
651+
new MessageEncoderSettings());
652+
var commandResult = commandOperation.Execute(binding, CancellationToken.None);
653+
return commandResult["cursor"]["firstBatch"].AsBsonArray.Where(c => c["name"] == _collectionNamespace.CollectionName).Single().AsBsonDocument;
654+
}
655+
629656
private BsonDocument ExecuteOperation(CreateCollectionOperation subject, IWriteBinding binding, bool async)
630657
{
631658
if (async)

src/MongoDB.Driver.Legacy.Tests/Builders/CollectionOptionsBuilderTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ public void TestSetCappedTrue()
6666
Assert.AreEqual(expected, options.ToJson());
6767
}
6868

69+
[Test]
70+
public void TestSetIndexOptionDefaults()
71+
{
72+
var options = CollectionOptions.SetIndexOptionDefaults(new IndexOptionDefaults { StorageEngine = new BsonDocument("mmapv1", new BsonDocument()) });
73+
var expected = "{ \"indexOptionDefaults\" : { \"storageEngine\" : { \"mmapv1\" : { } } } }";
74+
Assert.AreEqual(expected, options.ToJson());
75+
}
76+
6977
[Test]
7078
public void TestSetMaxDocuments()
7179
{

src/MongoDB.Driver.Legacy.Tests/MongoDatabaseTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,25 @@ public void TestCreateCollection()
7272
Assert.IsTrue(_database.CollectionExists(collectionName));
7373
}
7474

75+
[Test]
76+
[RequiresServer(MinimumVersion = "3.2.0-rc0")]
77+
public void TestCreateCollectionSetIndexOptionDefaults()
78+
{
79+
var collection = _database.GetCollection("testindexoptiondefaults");
80+
collection.Drop();
81+
Assert.IsFalse(collection.Exists());
82+
var storageEngineOptions = new BsonDocument("mmapv1", new BsonDocument());
83+
var indexOptionDefaults = new IndexOptionDefaults { StorageEngine = storageEngineOptions };
84+
var expectedIndexOptionDefaultsDocument = new BsonDocument("storageEngine", storageEngineOptions);
85+
var options = CollectionOptions.SetIndexOptionDefaults(indexOptionDefaults);
86+
87+
_database.CreateCollection(collection.Name, options);
88+
89+
var commandResult = _database.RunCommand("listCollections");
90+
var collectionInfo = commandResult.Response["cursor"]["firstBatch"].AsBsonArray.Where(doc => doc["name"] == collection.Name).Single().AsBsonDocument;
91+
Assert.AreEqual(expectedIndexOptionDefaultsDocument, collectionInfo["options"]["indexOptionDefaults"]);
92+
}
93+
7594
[Test]
7695
[RequiresServer(MinimumVersion = "2.7.0")]
7796
public void TestCreateCollectionSetStorageEngine()

src/MongoDB.Driver.Legacy/Builders/CollectionOptionsBuilder.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ public static CollectionOptionsBuilder SetCapped(bool value)
5656
return new CollectionOptionsBuilder().SetCapped(value);
5757
}
5858

59+
/// <summary>
60+
/// Sets the index options defaults.
61+
/// </summary>
62+
/// <returns>The builder (so method calls can be chained).</returns>
63+
/// <returns></returns>
64+
public static CollectionOptionsBuilder SetIndexOptionDefaults(IndexOptionDefaults value)
65+
{
66+
return new CollectionOptionsBuilder().SetIndexOptionDefaults(value);
67+
}
68+
5969
/// <summary>
6070
/// Sets the max number of documents in a capped collection.
6171
/// </summary>
@@ -158,6 +168,17 @@ public CollectionOptionsBuilder SetCapped(bool value)
158168
return this;
159169
}
160170

171+
/// <summary>
172+
/// Sets the index options defaults.
173+
/// </summary>
174+
/// <param name="value">The index options defaults.</param>
175+
/// <returns>The builder (so method calls can be chained).</returns>
176+
public CollectionOptionsBuilder SetIndexOptionDefaults(IndexOptionDefaults value)
177+
{
178+
_document["indexOptionDefaults"] = value.ToBsonDocument();
179+
return this;
180+
}
181+
161182
/// <summary>
162183
/// Sets the max number of documents in a capped collection.
163184
/// </summary>

src/MongoDB.Driver.Legacy/MongoDatabase.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ public virtual CommandResult CreateCollection(string collectionName, IMongoColle
207207
var messageEncoderSettings = GetMessageEncoderSettings();
208208
bool? autoIndexId = null;
209209
bool? capped = null;
210+
BsonDocument indexOptionDefaults = null;
210211
int? maxDocuments = null;
211212
long? maxSize = null;
212213
BsonDocument storageEngine = null;
@@ -228,6 +229,10 @@ public virtual CommandResult CreateCollection(string collectionName, IMongoColle
228229
{
229230
capped = value.ToBoolean();
230231
}
232+
if (optionsDocument.TryGetValue("indexOptionDefaults", out value))
233+
{
234+
indexOptionDefaults = value.AsBsonDocument;
235+
}
231236
if (optionsDocument.TryGetValue("max", out value))
232237
{
233238
maxDocuments = value.ToInt32();
@@ -262,6 +267,7 @@ public virtual CommandResult CreateCollection(string collectionName, IMongoColle
262267
{
263268
AutoIndexId = autoIndexId,
264269
Capped = capped,
270+
IndexOptionDefaults = indexOptionDefaults,
265271
MaxDocuments = maxDocuments,
266272
MaxSize = maxSize,
267273
StorageEngine = storageEngine,

0 commit comments

Comments
 (0)