Hi there folks. I’m working in a migration that will update (updateMany) a couple of millions of documents, with some basic evaluation based on some existing fields. To do that, I will use bulkWrite.
The problem is, for these evaluations, I would like to use $switch or if conditions, but it seems it doesn’t like it inside of the structure of bulkWrite. The query I have is the following:
const result = await mongo.collection('tickets').bulkWrite([
{
updateMany: {
filter: {
$or: [{ 'fsm.state': null }, { 'fsm.state': { $exists: false } }]
},
update: {
$set: {
'fsm.state': {
$switch:
{
branches: [
{
case: { endedAt: { $ne: null } },
then: "closed"
},
{
case: { $and: [{ followUp: { $ne: null } }, { 'followUp.pending': true }] },
then: "pending_follow_up"
},
{
case: { $and: [{ review: { $ne: null } }, { 'review.pending': true }] },
then: "pending_feedback"
}
],
default: "chat_in_progress"
}
}
},
$unset: {
state: ''
}
},
upsert: false
}
}
],
{
ordered: false
}
);
for what I get the error The dollar ($) prefixed field '$switch' in 'fsm.state.$switch' is not valid for storage
. So searching a bit I found this topic (MongoError: The dollar ($) prefixed field ‘$cond’ in ‘energy.$cond’ is not valid for storage) which suggests to put the contents of update in an array, for which it throws the following error:
BSON field 'update.updates.u' is the wrong type 'array', expected type 'object'
Is there a way to achieve this?