You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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!
Why strict is boolean for Model.bulkWrite options? Why there is no throw option like in schema?
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?
The text was updated successfully, but these errors were encountered:
For the ordered: false behavior, you should set the throwOnValidationError option as shown below:
'use strict';constmongoose=require('mongoose');constTestModel=mongoose.model('Test',mongoose.Schema({name: String}));run().catch(err=>console.log(err));asyncfunctionrun(){awaitmongoose.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"constres=awaitTestModel.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
added
the
docs
This issue is due to a mistake or omission in the mongoosejs.com documentation
label
Aug 16, 2024
Prerequisites
Mongoose version
7.6.11
Node.js version
18
MongoDB server version
5.0
Typescript version (if applicable)
No response
Description
Hi!
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?
The text was updated successfully, but these errors were encountered: