Skip to content

Commit a826473

Browse files
author
Divjot Arora
committed
Modify aggregate to only encode a write concern if $out is specified.
GODRIVER-666 Change-Id: Icfbb86359ce3e3a2b5678a75c3727993053ccdcc
1 parent 2bcd97a commit a826473

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

x/network/command/aggregate.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,12 @@ func (a *Aggregate) encode(desc description.SelectedServer) (*Read, error) {
5858
}
5959

6060
cursor := bsonx.Doc{}
61+
hasOutStage := a.HasDollarOut()
6162

6263
for _, opt := range a.Opts {
6364
switch opt.Key {
6465
case "batchSize":
65-
if opt.Value.Int32() == 0 && a.HasDollarOut() {
66+
if opt.Value.Int32() == 0 && hasOutStage {
6667
continue
6768
}
6869
cursor = append(cursor, opt)
@@ -73,7 +74,7 @@ func (a *Aggregate) encode(desc description.SelectedServer) (*Read, error) {
7374
command = append(command, bsonx.Elem{"cursor", bsonx.Document(cursor)})
7475

7576
// add write concern because it won't be added by the Read command's Encode()
76-
if a.WriteConcern != nil {
77+
if desc.WireVersion.Max >= 5 && hasOutStage && a.WriteConcern != nil {
7778
element, err := a.WriteConcern.MarshalBSONElement()
7879
if err != nil {
7980
return nil, err

x/network/command/aggregate_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package command
2+
3+
import (
4+
"github.com/mongodb/mongo-go-driver/internal/testutil/helpers"
5+
"github.com/mongodb/mongo-go-driver/mongo/writeconcern"
6+
"github.com/mongodb/mongo-go-driver/x/bsonx"
7+
"github.com/mongodb/mongo-go-driver/x/network/description"
8+
"testing"
9+
)
10+
11+
func TestAggregate(t *testing.T) {
12+
wc := writeconcern.New(writeconcern.W(10))
13+
legacyDesc := description.SelectedServer{
14+
Server: description.Server{
15+
WireVersion: &description.VersionRange{
16+
Max: 4,
17+
},
18+
},
19+
}
20+
desc := description.SelectedServer{
21+
Server: description.Server{
22+
WireVersion: &description.VersionRange{
23+
Max: 5,
24+
},
25+
},
26+
}
27+
outDoc := bsonx.Doc{{"$out", bsonx.Int32(1)}}
28+
outPipeline := bsonx.Arr{bsonx.Document(outDoc)}
29+
30+
testCases := []struct {
31+
name string
32+
desc description.SelectedServer
33+
pipeline bsonx.Arr
34+
wcExpected bool
35+
}{
36+
{"LegacyDescNoOut", legacyDesc, bsonx.Arr{}, false},
37+
{"LegacyDescOut", legacyDesc, outPipeline, false},
38+
{"NewDescNoOut", desc, bsonx.Arr{}, false},
39+
{"NewDescOut", desc, outPipeline, true},
40+
}
41+
42+
for _, tc := range testCases {
43+
t.Run(tc.name, func(t *testing.T) {
44+
cmd := Aggregate{
45+
NS: Namespace{
46+
DB: "db",
47+
Collection: "coll",
48+
},
49+
Pipeline: tc.pipeline,
50+
WriteConcern: wc,
51+
}
52+
53+
readCmd, err := cmd.encode(tc.desc)
54+
testhelpers.RequireNil(t, err, "error encoding: %s", err)
55+
56+
_, err = readCmd.Command.LookupErr("writeConcern")
57+
nilErr := err == nil
58+
// err should be nil if wc was expected
59+
if nilErr != tc.wcExpected {
60+
t.Fatalf("write concern mismatch: expected %v got %v", tc.wcExpected, nilErr)
61+
}
62+
})
63+
}
64+
}

0 commit comments

Comments
 (0)