Skip to content

Commit 81144c3

Browse files
committed
CSHARP-1413: Add SetIndexOptionDefaults to CollectionOptionsBuilder.
1 parent ea63289 commit 81144c3

File tree

5 files changed

+100
-0
lines changed

5 files changed

+100
-0
lines changed

MongoDB.Driver/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>

MongoDB.Driver/IndexOptionDefaults.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* Copyright 2015 MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using MongoDB.Bson;
17+
18+
namespace MongoDB.Driver
19+
{
20+
/// <summary>
21+
/// Represents index option defaults.
22+
/// </summary>
23+
public class IndexOptionDefaults
24+
{
25+
// private fields
26+
private BsonDocument _storageEngine;
27+
28+
// public properties
29+
/// <summary>
30+
/// Gets or sets the storage engine options.
31+
/// </summary>
32+
public BsonDocument StorageEngine
33+
{
34+
get { return _storageEngine; }
35+
set { _storageEngine = value; }
36+
}
37+
38+
// internal methods
39+
/// <summary>
40+
/// Returns this instance represented as a BsonDocument.
41+
/// </summary>
42+
/// <returns>A BsonDocument.</returns>
43+
internal BsonDocument ToBsonDocument()
44+
{
45+
return new BsonDocument
46+
{
47+
{ "storageEngine", _storageEngine, _storageEngine != null }
48+
};
49+
}
50+
}
51+
}

MongoDB.Driver/MongoDB.Driver.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
<Compile Include="Exceptions\MongoBulkWriteException.cs" />
9696
<Compile Include="Exceptions\MongoExecutionTimeoutException.cs" />
9797
<Compile Include="Exceptions\MongoWriteConcernException.cs" />
98+
<Compile Include="IndexOptionDefaults.cs" />
9899
<Compile Include="Operations\ListCollectionsOperation.cs" />
99100
<Compile Include="Operations\ListIndexesOperation.cs" />
100101
<Compile Include="Operations\ParallelScanOperation.cs" />

MongoDB.DriverUnitTests/Builders/CollectionOptionsBuilderTests.cs

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

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

MongoDB.DriverUnitTests/MongoDatabaseTests.cs

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

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

0 commit comments

Comments
 (0)