-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
bulkWrite doesn't respect extended schema timestamp option #13409
Labels
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
Milestone
Comments
IslandRhythms
added
the
typescript
Types or Types-test related issue / Pull Request
label
May 19, 2023
import { Schema, model, models, Model, connection, connect } from 'mongoose';
export interface BaseShape {
readonly createdAt: Date;
readonly updatedAt: Date;
}
export const baseOptions = { timestamps: true };
export const BaseSchema = new Schema<BaseShape>({}, baseOptions);
interface ExtendedShape extends BaseShape {
name: string;
}
const extendedSchema = new Schema<ExtendedShape>({
name: { type: String },
}).add(BaseSchema);
// workaround
// const extendedSchema = new Schema<ExtendedShape>(
// {
// name: { type: String },
// },
// baseOptions,
// ).add(BaseSchema);
export const Extended = (models.Extended ||
model<ExtendedShape>(
"Extended",
extendedSchema,
)) as Model<ExtendedShape>;
async function run() {
await connect('mongodb://localhost:27017');
await connection.dropDatabase();
const doc = await Extended.create({ name: 'Test' });
console.log('what is doc', doc);
console.log('=============================================')
const upRes = await Extended.updateOne({ name: 'Test Testerson Single'});
console.log(await Extended.findOne());
console.log('===========================================')
for (let i = 0; i < 10; i++) {
// a small delay
console.log(i)
}
const bulkRes = await Extended.bulkWrite([{ updateOne: {
filter: { name: 'Test Testerson Single' },
update: { name: 'Test Testerson Bulk'}
}}]);
console.log(await Extended.findOne());
}
run(); |
vkarpov15
added
has repro script
There is a repro script, the Mongoose devs need to confirm that it reproduces the issue
and removed
typescript
Types or Types-test related issue / Pull Request
labels
May 23, 2023
vkarpov15
added
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
and removed
has repro script
There is a repro script, the Mongoose devs need to confirm that it reproduces the issue
labels
May 26, 2023
vkarpov15
added a commit
that referenced
this issue
May 26, 2023
…stamps option when set by merging schemas Fix #13409
vkarpov15
added a commit
that referenced
this issue
May 27, 2023
fix(schema): make bulkWrite updateOne() and updateMany() respect timestamps option when set by merging schemas
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Prerequisites
Mongoose version
7.1.1
Node.js version
16.19
MongoDB server version
5.3
Typescript version (if applicable)
4.9.5
Description
Schema.bulkWrite
does not update the record timestamps when another base schema has been added to it that has thetimestamps
option set totrue
Steps to Reproduce
Example schema design:
In the example above. calling
Extended.updateOne
will update the timestamps. However, callingExtended.bulkWrite
with theupdateOne
orupdateMany
option, does not update the timestamps. I have a current workaround to pass the same options:Expected Behavior
Schema.bulkWrite
andSchema.updateOne
should behave the same. Ideally, they both respect the base optionsThe text was updated successfully, but these errors were encountered: