Skip to content

Commit 14e7c52

Browse files
author
rstam
committed
CSHARP-956: Aggregate command with $out cannot be sent to secondary.
1 parent fd47b44 commit 14e7c52

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

MongoDB.Driver/CanCommandBeSentToSecondary.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System;
1717
using System.Collections;
1818
using System.Collections.Generic;
19+
using System.Linq;
1920
using MongoDB.Bson;
2021
using MongoDB.Driver.Internal;
2122

@@ -30,7 +31,6 @@ public static class CanCommandBeSentToSecondary
3031
private static Func<BsonDocument, bool> __delegate = DefaultImplementation;
3132
private static HashSet<string> __secondaryOkCommands = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase)
3233
{
33-
"aggregate",
3434
"collStats",
3535
"dbStats",
3636
"count",
@@ -80,6 +80,17 @@ public static bool DefaultImplementation(BsonDocument document)
8080
return true;
8181
}
8282

83+
if (commandName.Equals("aggregate", StringComparison.InvariantCultureIgnoreCase))
84+
{
85+
var pipeline = document["pipeline"].AsBsonArray;
86+
if (pipeline.Any(s => s.AsBsonDocument.GetElement(0).Name == "$out"))
87+
{
88+
return false;
89+
}
90+
91+
return true;
92+
}
93+
8394
if (commandName.Equals("mapreduce", StringComparison.InvariantCultureIgnoreCase))
8495
{
8596
BsonValue outValue;

MongoDB.DriverUnitTests/CanCommandBeSentToSecondaryTests.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System;
1717
using MongoDB.Bson;
1818
using MongoDB.Driver;
19+
using MongoDB.Driver.Builders;
1920
using NUnit.Framework;
2021

2122
namespace MongoDB.DriverUnitTests
@@ -39,7 +40,22 @@ public void TestSetDelegate()
3940
CanCommandBeSentToSecondary.Delegate = CanCommandBeSentToSecondary.DefaultImplementation; // reset Delegate
4041
}
4142

42-
[TestCase("aggregate", true)]
43+
[Test]
44+
public void TestCanSendAggregateWithReadOnlyPipelineToSecondary()
45+
{
46+
var pipeline = new BsonArray
47+
{
48+
new BsonDocument { { "$match", new BsonDocument("x", 1) } }
49+
};
50+
var command = new BsonDocument
51+
{
52+
{ "aggregate", "collection" },
53+
{ "pipeline", pipeline }
54+
};
55+
var result = CanCommandBeSentToSecondary.DefaultImplementation(command);
56+
Assert.IsTrue(result);
57+
}
58+
4359
[TestCase("collStats", true)]
4460
[TestCase("dbStats", true)]
4561
[TestCase("count", true)]
@@ -76,6 +92,23 @@ public void TestCanSendInlineMapReduceToSecondary()
7692
Assert.IsTrue(result);
7793
}
7894

95+
[Test]
96+
public void TestCannotSendAggregateWithWritePipelineToSecondary()
97+
{
98+
var pipeline = new BsonArray
99+
{
100+
new BsonDocument { { "$match", new BsonDocument("x", 1) } },
101+
new BsonDocument { { "$out", "output" } }
102+
};
103+
var command = new BsonDocument
104+
{
105+
{ "aggregate", "collection" },
106+
{ "pipeline", pipeline }
107+
};
108+
var result = CanCommandBeSentToSecondary.DefaultImplementation(command);
109+
Assert.IsFalse(result);
110+
}
111+
79112
[Test]
80113
public void TestCannotSendNonInlineMapReduceToSecondary()
81114
{

0 commit comments

Comments
 (0)