Skip to content

Commit 6e768b9

Browse files
committed
CSHARP-1424: Support PartialFilterExpression in CreateIndex.
1 parent 36a8f0d commit 6e768b9

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

MongoDB.Driver/Builders/IndexOptionsBuilder.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ public static IndexOptionsBuilder SetName(string value)
9999
return new IndexOptionsBuilder().SetName(value);
100100
}
101101

102+
/// <summary>
103+
/// Sets the partial filter expression.
104+
/// </summary>
105+
/// <param name="value">The value.</param>
106+
/// <returns>The builder (so method calls can be chained).</returns>
107+
public static IndexOptionsBuilder SetPartialFilterExpression(IMongoQuery value)
108+
{
109+
return new IndexOptionsBuilder().SetPartialFilterExpression(value);
110+
}
111+
102112
/// <summary>
103113
/// Sets whether the index is a sparse index.
104114
/// </summary>
@@ -260,6 +270,17 @@ public IndexOptionsBuilder SetName(string value)
260270
return this;
261271
}
262272

273+
/// <summary>
274+
/// Sets the partial filter expression.
275+
/// </summary>
276+
/// <param name="value">The value.</param>
277+
/// <returns>The builder (so method calls can be chained).</returns>
278+
public IndexOptionsBuilder SetPartialFilterExpression(IMongoQuery value)
279+
{
280+
_document["partialFilterExpression"] = value.ToBsonDocument();
281+
return this;
282+
}
283+
263284
/// <summary>
264285
/// Sets whether the index is a sparse index.
265286
/// </summary>
@@ -445,6 +466,16 @@ public static IndexOptionsBuilder<TDocument> SetName(string value)
445466
return new IndexOptionsBuilder<TDocument>().SetName(value);
446467
}
447468

469+
/// <summary>
470+
/// Sets the partial filter expression.
471+
/// </summary>
472+
/// <param name="value">The value.</param>
473+
/// <returns>The builder (so method calls can be chained).</returns>
474+
public static IndexOptionsBuilder<TDocument> SetPartialFilterExpression(IMongoQuery value)
475+
{
476+
return new IndexOptionsBuilder<TDocument>().SetPartialFilterExpression(value);
477+
}
478+
448479
/// <summary>
449480
/// Sets whether the index is a sparse index.
450481
/// </summary>
@@ -611,6 +642,17 @@ public IndexOptionsBuilder<TDocument> SetName(string value)
611642
return this;
612643
}
613644

645+
/// <summary>
646+
/// Sets the partial filter expression.
647+
/// </summary>
648+
/// <param name="value">The value.</param>
649+
/// <returns>The builder (so method calls can be chained).</returns>
650+
public IndexOptionsBuilder<TDocument> SetPartialFilterExpression(IMongoQuery value)
651+
{
652+
_indexOptionsBuilder.SetPartialFilterExpression(value);
653+
return this;
654+
}
655+
614656
/// <summary>
615657
/// Sets whether the index is a sparse index.
616658
/// </summary>

MongoDB.DriverUnitTests/Builders/IndexOptionsBuilderTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ public void TestName()
6363
Assert.AreEqual(expected, options.ToJson());
6464
}
6565

66+
[Test]
67+
public void TestPartialFilterExpression()
68+
{
69+
var options = IndexOptions.SetPartialFilterExpression(Query.GT("x", 0));
70+
string expected = "{ \"partialFilterExpression\" : { \"x\" : { \"$gt\" : 0 } } }";
71+
Assert.AreEqual(expected, options.ToJson());
72+
}
73+
6674
[Test]
6775
public void TestSparse()
6876
{

MongoDB.DriverUnitTests/Builders/IndexOptionsBuilderTypedTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ public void TestName()
7171
Assert.AreEqual(expected, options.ToJson());
7272
}
7373

74+
[Test]
75+
public void TestPartialFilterExpression()
76+
{
77+
var options = IndexOptions<TestClass>.SetPartialFilterExpression(Query.GT("x", 0));
78+
string expected = "{ \"partialFilterExpression\" : { \"x\" : { \"$gt\" : 0 } } }";
79+
Assert.AreEqual(expected, options.ToJson());
80+
}
81+
7482
[Test]
7583
public void TestSparse()
7684
{

MongoDB.DriverUnitTests/MongoCollectionTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,21 @@ public void TestCreateIndexWithStorageOptions()
714714
Assert.AreEqual(2, result.Count);
715715
}
716716

717+
[Test]
718+
[RequiresServer(MinimumVersion = "3.1.9999")]
719+
public void TestCreateIndexWithPartialFilterExpression()
720+
{
721+
_collection.Drop();
722+
var keys = IndexKeys.Ascending("x");
723+
var options = IndexOptions<BsonDocument>.SetPartialFilterExpression(Query.GT("x", 0));
724+
725+
_collection.CreateIndex(keys, options);
726+
727+
var indexes = _collection.GetIndexes();
728+
var index = indexes.Where(i => i.Name == "x_1").Single();
729+
Assert.That(index.RawDocument["partialFilterExpression"], Is.EqualTo(BsonDocument.Parse("{ x : { $gt : 0 } }")));
730+
}
731+
717732
[Test]
718733
public void TestDistinct()
719734
{

0 commit comments

Comments
 (0)