Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/db-mongodb/src/updateJobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@ export const updateJobs: UpdateJobs = async function updateMany(

const $inc: Record<string, number> = {}
const $push: Record<string, { $each: any[] } | any> = {}
const $addToSet: Record<string, { $each: any[] } | any> = {}
const $pull: Record<string, { $in: any[] } | any> = {}

transform({
$addToSet,
$inc,
$pull,
$push,
adapter: this,
data,
Expand All @@ -73,6 +77,12 @@ export const updateJobs: UpdateJobs = async function updateMany(
if (Object.keys($push).length) {
updateOps.$push = $push
}
if (Object.keys($addToSet).length) {
updateOps.$addToSet = $addToSet
}
if (Object.keys($pull).length) {
updateOps.$pull = $pull
}
if (Object.keys(updateOps).length) {
updateOps.$set = updateData
updateData = updateOps
Expand Down
37 changes: 35 additions & 2 deletions packages/db-mongodb/src/updateMany.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { MongooseUpdateQueryOptions } from 'mongoose'
import type { MongooseUpdateQueryOptions, UpdateQuery } from 'mongoose'

import { flattenWhereToOperators, type UpdateMany } from 'payload'

Expand Down Expand Up @@ -70,7 +70,40 @@ export const updateMany: UpdateMany = async function updateMany(
where,
})

transform({ adapter: this, data, fields: collectionConfig.fields, operation: 'write' })
const $inc: Record<string, number> = {}
const $push: Record<string, { $each: any[] } | any> = {}
const $addToSet: Record<string, { $each: any[] } | any> = {}
const $pull: Record<string, { $in: any[] } | any> = {}

transform({
$addToSet,
$inc,
$pull,
$push,
adapter: this,
data,
fields: collectionConfig.fields,
operation: 'write',
})

const updateOps: UpdateQuery<any> = {}

if (Object.keys($inc).length) {
updateOps.$inc = $inc
}
if (Object.keys($push).length) {
updateOps.$push = $push
}
if (Object.keys($addToSet).length) {
updateOps.$addToSet = $addToSet
}
if (Object.keys($pull).length) {
updateOps.$pull = $pull
}
if (Object.keys(updateOps).length) {
updateOps.$set = data
data = updateOps
}

try {
if (typeof limit === 'number' && limit > 0) {
Expand Down
19 changes: 18 additions & 1 deletion packages/db-mongodb/src/updateOne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,19 @@ export const updateOne: UpdateOne = async function updateOne(

const $inc: Record<string, number> = {}
const $push: Record<string, { $each: any[] } | any> = {}
const $addToSet: Record<string, { $each: any[] } | any> = {}
const $pull: Record<string, { $in: any[] } | any> = {}

transform({ $inc, $push, adapter: this, data, fields, operation: 'write' })
transform({
$addToSet,
$inc,
$pull,
$push,
adapter: this,
data,
fields,
operation: 'write',
})

const updateOps: UpdateQuery<any> = {}

Expand All @@ -67,6 +78,12 @@ export const updateOne: UpdateOne = async function updateOne(
if (Object.keys($push).length) {
updateOps.$push = $push
}
if (Object.keys($addToSet).length) {
updateOps.$addToSet = $addToSet
}
if (Object.keys($pull).length) {
updateOps.$pull = $pull
}
if (Object.keys(updateOps).length) {
updateOps.$set = updateData
updateData = updateOps
Expand Down
Loading
Loading