1- import type { MongooseUpdateQueryOptions , UpdateQuery } from 'mongoose'
1+ import type { QueryOptions , UpdateQuery } from 'mongoose'
22import type { Job , UpdateJobs , Where } from 'payload'
33
44import type { MongooseAdapter } from './index.js'
@@ -80,33 +80,37 @@ export const updateJobs: UpdateJobs = async function updateMany(
8080 updateData = updateOps
8181 }
8282
83- const options : MongooseUpdateQueryOptions = {
84- lean : true ,
85- new : true ,
83+ const baseOptions = {
8684 session : await getSession ( this , req ) ,
8785 // Timestamps are manually added by the write transform
8886 timestamps : false ,
87+ } satisfies QueryOptions
88+
89+ const findOptions : QueryOptions = {
90+ ...baseOptions ,
91+ lean : true ,
92+ new : true ,
8993 }
9094
9195 let result : Job [ ] = [ ]
9296
9397 try {
9498 if ( id ) {
9599 if ( returning === false ) {
96- await Model . updateOne ( query , updateData , options )
100+ await Model . updateOne ( query , updateData , baseOptions )
97101 transform ( { adapter : this , data, fields : collectionConfig . fields , operation : 'read' } )
98102
99103 return null
100104 } else {
101- const doc = await Model . findOneAndUpdate ( query , updateData , options )
105+ const doc = await Model . findOneAndUpdate ( query , updateData , findOptions )
102106 result = doc ? [ doc ] : [ ]
103107 }
104108 } else {
105109 if ( typeof limit === 'number' && limit > 0 ) {
106110 const documentsToUpdate = await Model . find (
107111 query ,
108112 { } ,
109- { ...options , limit, projection : { _id : 1 } , sort } ,
113+ { ...findOptions , limit, projection : { _id : 1 } , sort } ,
110114 )
111115 if ( documentsToUpdate . length === 0 ) {
112116 return null
@@ -115,20 +119,13 @@ export const updateJobs: UpdateJobs = async function updateMany(
115119 query = { _id : { $in : documentsToUpdate . map ( ( doc ) => doc . _id ) } }
116120 }
117121
118- await Model . updateMany ( query , updateData , options )
122+ await Model . updateMany ( query , updateData , baseOptions )
119123
120124 if ( returning === false ) {
121125 return null
122126 }
123127
124- result = await Model . find (
125- query ,
126- { } ,
127- {
128- ...options ,
129- sort,
130- } ,
131- )
128+ result = await Model . find ( query , { } , { ...findOptions , sort } )
132129 }
133130 } catch ( error ) {
134131 handleError ( { collection : collectionConfig . slug , error, req } )
0 commit comments