@@ -863,6 +863,15 @@ func aggregate(a aggregateParams) (cur *Cursor, err error) {
863863 Timeout (a .client .timeout ).
864864 MaxTime (ao .MaxTime )
865865
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+
866875 if ao .AllowDiskUse != nil {
867876 op .AllowDiskUse (* ao .AllowDiskUse )
868877 }
@@ -1191,6 +1200,22 @@ func (coll *Collection) Distinct(ctx context.Context, fieldName string, filter i
11911200// For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/find/.
11921201func (coll * Collection ) Find (ctx context.Context , filter interface {},
11931202 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 ) {
11941219
11951220 if ctx == nil {
11961221 ctx = context .Background ()
@@ -1230,7 +1255,8 @@ func (coll *Collection) Find(ctx context.Context, filter interface{},
12301255 CommandMonitor (coll .client .monitor ).ServerSelector (selector ).
12311256 ClusterClock (coll .client .clock ).Database (coll .db .name ).Collection (coll .name ).
12321257 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 )
12341260
12351261 cursorOpts := coll .client .createBaseCursorOptions ()
12361262
@@ -1408,7 +1434,7 @@ func (coll *Collection) FindOne(ctx context.Context, filter interface{},
14081434 // by the server.
14091435 findOpts = append (findOpts , options .Find ().SetLimit (- 1 ))
14101436
1411- cursor , err := coll .Find (ctx , filter , findOpts ... )
1437+ cursor , err := coll .find (ctx , filter , false , findOpts ... )
14121438 return & SingleResult {
14131439 ctx : ctx ,
14141440 cur : cursor ,
0 commit comments