@@ -863,6 +863,15 @@ func aggregate(a aggregateParams) (cur *Cursor, err error) {
863
863
Timeout (a .client .timeout ).
864
864
MaxTime (ao .MaxTime )
865
865
866
+ // Omit "maxTimeMS" from operations that return a user-managed cursor to
867
+ // prevent confusing "cursor not found" errors. To maintain existing
868
+ // behavior for users who set "timeoutMS" with no context deadline, only
869
+ // omit "maxTimeMS" when a context deadline is set.
870
+ //
871
+ // See DRIVERS-2722 for more detail.
872
+ _ , deadlineSet := a .ctx .Deadline ()
873
+ op .OmitCSOTMaxTimeMS (deadlineSet )
874
+
866
875
if ao .AllowDiskUse != nil {
867
876
op .AllowDiskUse (* ao .AllowDiskUse )
868
877
}
@@ -1191,6 +1200,22 @@ func (coll *Collection) Distinct(ctx context.Context, fieldName string, filter i
1191
1200
// For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/find/.
1192
1201
func (coll * Collection ) Find (ctx context.Context , filter interface {},
1193
1202
opts ... * options.FindOptions ) (cur * Cursor , err error ) {
1203
+ // Omit "maxTimeMS" from operations that return a user-managed cursor to
1204
+ // prevent confusing "cursor not found" errors. To maintain existing
1205
+ // behavior for users who set "timeoutMS" with no context deadline, only
1206
+ // omit "maxTimeMS" when a context deadline is set.
1207
+ //
1208
+ // See DRIVERS-2722 for more detail.
1209
+ _ , deadlineSet := ctx .Deadline ()
1210
+ return coll .find (ctx , filter , deadlineSet , opts ... )
1211
+ }
1212
+
1213
+ func (coll * Collection ) find (
1214
+ ctx context.Context ,
1215
+ filter interface {},
1216
+ omitCSOTMaxTimeMS bool ,
1217
+ opts ... * options.FindOptions ,
1218
+ ) (cur * Cursor , err error ) {
1194
1219
1195
1220
if ctx == nil {
1196
1221
ctx = context .Background ()
@@ -1230,7 +1255,8 @@ func (coll *Collection) Find(ctx context.Context, filter interface{},
1230
1255
CommandMonitor (coll .client .monitor ).ServerSelector (selector ).
1231
1256
ClusterClock (coll .client .clock ).Database (coll .db .name ).Collection (coll .name ).
1232
1257
Deployment (coll .client .deployment ).Crypt (coll .client .cryptFLE ).ServerAPI (coll .client .serverAPI ).
1233
- Timeout (coll .client .timeout ).MaxTime (fo .MaxTime ).Logger (coll .client .logger )
1258
+ Timeout (coll .client .timeout ).MaxTime (fo .MaxTime ).Logger (coll .client .logger ).
1259
+ OmitCSOTMaxTimeMS (omitCSOTMaxTimeMS )
1234
1260
1235
1261
cursorOpts := coll .client .createBaseCursorOptions ()
1236
1262
@@ -1408,7 +1434,7 @@ func (coll *Collection) FindOne(ctx context.Context, filter interface{},
1408
1434
// by the server.
1409
1435
findOpts = append (findOpts , options .Find ().SetLimit (- 1 ))
1410
1436
1411
- cursor , err := coll .Find (ctx , filter , findOpts ... )
1437
+ cursor , err := coll .find (ctx , filter , false , findOpts ... )
1412
1438
return & SingleResult {
1413
1439
ctx : ctx ,
1414
1440
cur : cursor ,
0 commit comments