Skip to content

Commit 207c735

Browse files
committed
CSHARP-1291: And and Or filter builder methods should handle zero clauses correctly.
1 parent 82b11c0 commit 207c735

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/MongoDB.Driver/FilterDefinitionBuilder.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,6 +1503,11 @@ public override BsonDocument Render(IBsonSerializer<TDocument> documentSerialize
15031503
}
15041504
}
15051505

1506+
if (document.ElementCount == 0)
1507+
{
1508+
document = new BsonDocument("$and", new BsonArray(0));
1509+
}
1510+
15061511
return document;
15071512
}
15081513

tests/MongoDB.Driver.Tests/FilterDefinitionBuilderTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,16 @@ public void And_with_a_nested_and_and_clashing_keys_using_ampersand()
131131
Assert(filter, "{$and: [{a: 1}, {a: 2}, {c: 3}]}");
132132
}
133133

134+
[Fact]
135+
public void And_with_no_clauses()
136+
{
137+
var subject = CreateSubject<BsonDocument>();
138+
139+
var filter = subject.And();
140+
141+
Assert(filter, "{ $and : [] }");
142+
}
143+
134144
[Fact]
135145
public void BitsAllClear()
136146
{
@@ -830,6 +840,26 @@ public void Or_should_flatten_nested_ors_with_a_pipe()
830840
Assert(filter, "{$or: [{a: 1}, {b: 2}, {c: 3}]}");
831841
}
832842

843+
[Fact]
844+
public void Or_with_no_clauses()
845+
{
846+
var subject = CreateSubject<BsonDocument>();
847+
848+
var filter = subject.Or();
849+
850+
Assert(filter, "{ $or : [] }");
851+
}
852+
853+
[Fact]
854+
public void Nor_with_no_clauses()
855+
{
856+
var subject = CreateSubject<BsonDocument>();
857+
858+
var filter = subject.Not(subject.Or());
859+
860+
Assert(filter, "{ $nor : [] }");
861+
}
862+
833863
[Fact]
834864
public void Regex()
835865
{

0 commit comments

Comments
 (0)