Thank you @steevej for your answer.
Here is the complete operations array building:
const operations = [
{
collection: 'questionnaires',
operation: 'updateMany',
filtres: {
_id: filtres.questionnaire_id,
},
champs: {
$pull: {
'questions.$[question].conditions': {
parent_id: champs.question_id,
},
},
$set: {
'questions.$[question].conditionnelle': {
$gt: [{ $size: '$questions.$[question].conditions' }, 0],
},
},
},
options: {
arrayFilters: [
{
'question._id': {
$in: questions_conditionnees,
},
},
],
},
},
{
collection: 'questionnaires',
operation: 'updateOne',
filtres: { _id: filtres.questionnaire_id },
champs: { $pull: { questions: { _id: champs.question_id } } },
},
]
This array is then passed to this function that makes use of a transaction:
function requete(operations) {
const session = client.startSession()
try {
const resultats = []
await session.withTransaction(async () => {
for (const operation of operations) {
const db = client.db(process.env.DB_NAME)
const col = db.collection(operation.collection)
await col[operation.operation](
operation.filtres,
operation.champs,
operation.options
).then(resultat => {
resultats.push(resultat)
})
}
})
return resultats
} catch (error) {
logger.error(error, {
requete: 'transaction.js',
operations: operations,
})
return error
} finally {
await session.endSession()
}
}
I have tried to change the following part, according to the doc you linked:
champs: [
{
$pull: {
'questions.$[question].conditions': {
parent_id: champs.question_id,
},
},
},
{
$set: {
'questions.$[question].conditionnelle': {
$gt: [
{ $size: '$questions.$[question].conditions' },
0,
],
},
},
},
],
But I then cannot use a $pull method, here is the error I get: “Unrecognized pipeline stage name: ‘$pull’”
Is there any solution to combine?