Skip to content

Commit 814779d

Browse files
authored
GODRIVER-1769 pass MaxTime from FindOne to Find (#525) (#527)
1 parent 68d5da1 commit 814779d

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

mongo/collection.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,6 +1269,7 @@ func (coll *Collection) FindOne(ctx context.Context, filter interface{},
12691269
Hint: opt.Hint,
12701270
Max: opt.Max,
12711271
MaxAwaitTime: opt.MaxAwaitTime,
1272+
MaxTime: opt.MaxTime,
12721273
Min: opt.Min,
12731274
NoCursorTimeout: opt.NoCursorTimeout,
12741275
OplogReplay: opt.OplogReplay,

mongo/integration/collection_test.go

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -776,17 +776,75 @@ func TestCollection(t *testing.T) {
776776
got := x.Int32()
777777
assert.Equal(mt, int32(1), got, "expected x value 1, got %v", got)
778778
})
779-
mt.Run("options", func(mt *mtest.T) {
779+
mt.RunOpts("options", mtest.NewOptions().MinServerVersion("3.4"), func(mt *mtest.T) {
780780
initCollection(mt, mt.Coll)
781-
opts := options.FindOne().SetComment("here's a query for ya")
782-
res, err := mt.Coll.FindOne(mtest.Background, bson.D{{"x", 1}}, opts).DecodeBytes()
781+
782+
// Create an index for Hint, Max, and Min options
783+
indexView := mt.Coll.Indexes()
784+
indexName, err := indexView.CreateOne(context.Background(), mongo.IndexModel{
785+
Keys: bson.D{{"x", 1}},
786+
})
787+
assert.Nil(mt, err, "CreateOne error: %v", err)
788+
789+
mt.ClearEvents()
790+
expectedComment := "here's a query for ya"
791+
// SetCursorType is excluded because tailable cursors can't be used with limit -1,
792+
// which all FindOne operations set, and the nontailable setting isn't passed to the
793+
// operation layer
794+
// SetMaxAwaitTime affects the cursor and not the server command, so it can't be checked
795+
// SetCursorTime and setMaxAwaitTime will be deprecated in GODRIVER-1775
796+
opts := options.FindOne().
797+
SetAllowPartialResults(true).
798+
SetBatchSize(2).
799+
SetCollation(&options.Collation{Locale: "en_US"}).
800+
SetComment(expectedComment).
801+
SetHint(indexName).
802+
SetMax(bson.D{{"x", int32(5)}}).
803+
SetMaxTime(1 * time.Second).
804+
SetMin(bson.D{{"x", int32(0)}}).
805+
SetNoCursorTimeout(false).
806+
SetOplogReplay(false).
807+
SetProjection(bson.D{{"x", int32(1)}}).
808+
SetReturnKey(false).
809+
SetShowRecordID(false).
810+
SetSkip(0).
811+
SetSort(bson.D{{"x", int32(1)}})
812+
res, err := mt.Coll.FindOne(mtest.Background, bson.D{}, opts).DecodeBytes()
783813
assert.Nil(mt, err, "FindOne error: %v", err)
784814

785815
x, err := res.LookupErr("x")
786816
assert.Nil(mt, err, "x not found in document %v", res)
787817
assert.Equal(mt, bson.TypeInt32, x.Type, "expected x type %v, got %v", bson.TypeInt32, x.Type)
788818
got := x.Int32()
789819
assert.Equal(mt, int32(1), got, "expected x value 1, got %v", got)
820+
821+
optionsDoc := bson.M{
822+
"allowPartialResults": true,
823+
"batchSize": int32(2),
824+
"collation": bson.M{"locale": "en_US"},
825+
"comment": expectedComment,
826+
"hint": indexName,
827+
"max": bson.M{"x": int32(5)},
828+
"maxTimeMS": int32(1000),
829+
"min": bson.M{"x": int32(0)},
830+
"noCursorTimeout": false,
831+
"oplogReplay": false,
832+
"projection": bson.M{"x": int32(1)},
833+
"returnKey": false,
834+
"showRecordId": false,
835+
"skip": int64(0),
836+
"sort": bson.M{"x": int32(1)},
837+
}
838+
expectedOpts, err := bson.Marshal(optionsDoc)
839+
assert.Nil(mt, err, "error marshaling expected options: %v", err)
840+
841+
started := mt.GetStartedEvent()
842+
assert.NotNil(mt, started, "expected CommandStartedEvent, got nil")
843+
844+
if err := compareDocs(mt, expectedOpts, started.Command); err != nil {
845+
mt.Fatalf("options mismatch: %v", err)
846+
}
847+
790848
})
791849
mt.Run("not found", func(mt *mtest.T) {
792850
initCollection(mt, mt.Coll)

0 commit comments

Comments
 (0)