@@ -1390,6 +1390,56 @@ describe('find', () => {
13901390 expect ( res . hasNext ) . toBe ( true ) ;
13911391 } ) ;
13921392
1393+ it ( 'pagination works with custom paginatedField and projection without _id' , async ( ) => {
1394+ const collection = t . db . collection ( 'test_paging_custom_fields' ) ;
1395+
1396+ // First page with projection that doesn't include _id
1397+ const firstPage = await paging . find ( collection , {
1398+ limit : 2 ,
1399+ fields : {
1400+ counter : 1 ,
1401+ } ,
1402+ paginatedField : 'timestamp' ,
1403+ } ) ;
1404+
1405+ expect ( firstPage . results . length ) . toEqual ( 2 ) ;
1406+ expect ( firstPage . results [ 0 ] . counter ) . toEqual ( 6 ) ;
1407+ expect ( firstPage . results [ 1 ] . counter ) . toEqual ( 5 ) ;
1408+ expect ( firstPage . results [ 0 ] . _id ) . toBe ( undefined ) ; // _id should not be in results
1409+ expect ( firstPage . results [ 0 ] . timestamp ) . toBe ( undefined ) ; // timestamp should not be in results
1410+ expect ( firstPage . hasNext ) . toBe ( true ) ;
1411+
1412+ // Second page - this is where the bug occurs
1413+ const secondPage = await paging . find ( collection , {
1414+ limit : 2 ,
1415+ fields : {
1416+ counter : 1 ,
1417+ } ,
1418+ paginatedField : 'timestamp' ,
1419+ next : firstPage . next ,
1420+ } ) ;
1421+
1422+ expect ( secondPage . results . length ) . toEqual ( 2 ) ;
1423+ expect ( secondPage . results [ 0 ] . counter ) . toEqual ( 4 ) ;
1424+ expect ( secondPage . results [ 1 ] . counter ) . toEqual ( 3 ) ;
1425+ expect ( secondPage . hasNext ) . toBe ( true ) ;
1426+
1427+ // Third page
1428+ const thirdPage = await paging . find ( collection , {
1429+ limit : 2 ,
1430+ fields : {
1431+ counter : 1 ,
1432+ } ,
1433+ paginatedField : 'timestamp' ,
1434+ next : secondPage . next ,
1435+ } ) ;
1436+
1437+ expect ( thirdPage . results . length ) . toEqual ( 2 ) ;
1438+ expect ( thirdPage . results [ 0 ] . counter ) . toEqual ( 2 ) ;
1439+ expect ( thirdPage . results [ 1 ] . counter ) . toEqual ( 1 ) ;
1440+ expect ( thirdPage . hasNext ) . toBe ( false ) ;
1441+ } ) ;
1442+
13931443 it ( 'does not overwrite $or used in a query with next/previous' , async ( ) => {
13941444 const collection = t . db . collection ( 'test_paging_custom_fields' ) ;
13951445 // First page of 2
0 commit comments