Mongoose runValidator with bulkWrite

Viewed 84

I'm having issue with validators while doing a bulkoperation

verificationcount: {
  type: Number,
  default: 0,
  min: 0,
  max: 2,
},
verificationfailedcount: {
  type: Number,
  default: 0,
  min: 0,
  max: 2,
},

max and min doesn't work probably because im doing a bulk operation.

Here is my code for the bulk operation :

  const myOperations = req.body.documents.map((doc) => ({
    updateOne: {
      filter: {
        _id: doc._id,
      },
      update: {
        $inc: {
          verificationcount: doc.verificationcount,
          verificationfailedcount: doc.verificationfailedcount,
        },
        $addToSet: { opinionby: req.profile._id },
        // increase vers
      },
    },
  }));

  Document.bulkWrite(myOperations, {}, (err, docs) => {
    if (err) {
      return res.status(200).json({
        status: 'fail',
        message:
          "Operazione di aggiornamento documenti non è andata buon fine, riprova piu' tardi!",
        error: 'Operazione di bulk fallità',
      });
    }

    // TODO: SEND EMAILS TO PARTS
    return res.status(200).json({
      status: 'success',
      message: 'Documenti aggiornati con successo',
      doc: docs,
    });
  });

How can i run validators on max and min?

0 Answers
Related