Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model.bulkWrite options strict weird behavior #14804

Closed
2 tasks done
MikeKoval opened this issue Aug 13, 2024 · 1 comment · Fixed by #14809
Closed
2 tasks done

Model.bulkWrite options strict weird behavior #14804

MikeKoval opened this issue Aug 13, 2024 · 1 comment · Fixed by #14809
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation

Comments

@MikeKoval
Copy link

MikeKoval commented Aug 13, 2024

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

7.6.11

Node.js version

18

MongoDB server version

5.0

Typescript version (if applicable)

No response

Description

Hi!

  1. Why strict is boolean for Model.bulkWrite options? Why there is no throw option like in schema?
  2. I am watching interesting behavior when schema has strict=throw.

Steps to Reproduce

Model.bulkWrite(ops) - throws strict error as expected when i try to $set field that is not in schema
but
Model.bulkWrite(ops, { ordered: false }) - not only does not throw an error but does nothing actually that is not clear really and i did not find that behavior in the documentation

if i provide strict=true to bulkWrite, then field that is not in schema will not be visible in mongoose logs, if i set strict=false, it will be visible, any of that options would be signficantly better than query is not executed and no error at all, but actually i am looking for an error in all places, no matter if its bulkWrite or single updateOne if that is possible...

Expected Behavior

if i take single updateOne from that bulkWrite and execute it via Model.updateOne, then i receive strict mode error as expected, why bulkWrite is working differently?

@vkarpov15
Copy link
Collaborator

For the ordered: false behavior, you should set the throwOnValidationError option as shown below:

'use strict';

const mongoose = require('mongoose');

const TestModel = mongoose.model('Test', mongoose.Schema({ name: String }));

run().catch(err => console.log(err));

async function run() {
  await mongoose.connect('mongodb://127.0.0.1:27017/mongoose_test');

  // Throws "MongooseBulkWriteError: bulkWrite failed with 1 Mongoose validation errors: Field `unknownProp` is not in schema and strict mode is set to throw"
  const res = await TestModel.bulkWrite([
    { updateOne: { filter: {}, update: { $set: { unknownProp: 'foo' } } } }
  ], { strict: 'throw', ordered: false, throwOnValidationError: true });
  console.log('Done', res);
}

By default, with ordered: false, Mongoose bulkWrite() will not throw an error if there were some operations that failed validation, but all valid operations executed successfully on the MongoDB server. This is one of those cases where it is tricky to define what behavior is correct: if some operations succeeded and some were never sent to the server because of validation errors, should we throw an error? throwOnValidationError makes it so that Mongoose does throw an error if some bulk write operations failed validation and were never sent.

@vkarpov15 vkarpov15 added the docs This issue is due to a mistake or omission in the mongoosejs.com documentation label Aug 16, 2024
vkarpov15 added a commit that referenced this issue Aug 19, 2024
docs(model): add 'throw' as valid strict value for bulkWrite() and add some more clarification on `throwOnValidationError`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Projects
None yet
2 participants