-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Versioning does not work if the versionKey
option for toObject
is set to false
when using insertMany
#14344
Comments
https://mongoosejs.com/docs/guide.html#versionKey According to the documentation, you shouldn't be putting versionKey inside toObject. You should put it in the Schema definition like
|
const mongoose = require('mongoose');
const testSchema = new mongoose.Schema(
{ name: String },
// Could be `optimisticConcurrency` as well
{ versionKey: "__v", toObject: { versionKey: false } }
);
const Test = mongoose.model("Test", testSchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
await Test.insertMany([{ name: 'x' }]);
const doc = await Test.findOne();
console.log(doc.__v);
console.log('done');
}
run(); |
@juona Does this solve your problem for now?
|
@donoftime2018 Thanks for the help. This was not a burning issue, I just used @vkarpov15 Thanks, swift as always! |
Prerequisites
Mongoose version
8.1.1
Node.js version
18.18.2
MongoDB server version
5
Typescript version (if applicable)
No response
Description
Quite a specific issue here...
Setting the schema option
toObject
to{ versionKey: false }
interferes with the versioning mechanism whenModel.prototype.insertMany()
is used. This affectsoptimisticConcurrency
too.Debug logs print:
Using
Model.prototype.create()
works correctly:Not surprising since the logic for the two methods is deliberately completely separate. Just makes me wonder if there are more edge cases like this one? I.e. what are some other ways of inserting a new document into the database that might not be covered? Perhaps this is something worth looking into.
Steps to Reproduce
Expected Behavior
My reasoning is that I set
toObject: { versionKey: false }
because I don't wish to have this field in the output when I invoketoObject()
. I don't believe it should affect the internal mechanics of the versioning feature though.The text was updated successfully, but these errors were encountered: