Skip to content

Commit e8ebcc3

Browse files
committed
CSHARP-1424: Support PartialFilterExpression in CreateIndex (2.x Legacy API).
1 parent bbd8a44 commit e8ebcc3

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

src/MongoDB.Driver.Legacy.Tests/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
{

src/MongoDB.Driver.Legacy.Tests/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
{

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,20 @@ public void TestCreateIndexWithStorageEngine()
688688
Assert.AreEqual(2, result.Count);
689689
}
690690

691+
[Test]
692+
public void TestCreateIndexWithPartialFilterExpression()
693+
{
694+
_collection.Drop();
695+
var keys = IndexKeys.Ascending("x");
696+
var options = IndexOptions<BsonDocument>.SetPartialFilterExpression(Query.GT("x", 0));
697+
698+
_collection.CreateIndex(keys, options);
699+
700+
var indexes = _collection.GetIndexes();
701+
var index = indexes.Where(i => i.Name == "x_1").Single();
702+
Assert.That(index.RawDocument["partialFilterExpression"], Is.EqualTo(BsonDocument.Parse("{ x : { $gt : 0 } }")));
703+
}
704+
691705
[Test]
692706
public void TestDistinct()
693707
{

src/MongoDB.Driver.Legacy/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>
@@ -261,6 +271,17 @@ public IndexOptionsBuilder SetName(string value)
261271
return this;
262272
}
263273

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

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

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

0 commit comments

Comments
 (0)