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

doc.set({ field: undefined }) does nothing if the schema has strict: false #15192

Closed
2 tasks done
lantw44 opened this issue Jan 22, 2025 · 0 comments · Fixed by #15207
Closed
2 tasks done

doc.set({ field: undefined }) does nothing if the schema has strict: false #15192

lantw44 opened this issue Jan 22, 2025 · 0 comments · Fixed by #15207
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@lantw44
Copy link
Contributor

lantw44 commented Jan 22, 2025

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.8.6

Node.js version

22.11.0

MongoDB server version

8.0.4

Typescript version (if applicable)

5.7.3

Description

If the schema has strict: false, you can't use doc.set({ field: undefined }) to unset a field. Instead, you have to use doc.set('field', undefined) or doc.field = undefined.

Steps to Reproduce

import mongoose from 'mongoose';

mongoose.connect('mongodb://localhost/test2');

const helloSchema = new mongoose.Schema({
  name: { type: String, required: true },
  status: { type: Boolean, required: true },
  optional: { type: Number },
}, { strict: false });
const Hello = mongoose.model('Hello', helloSchema);

const obj = new Hello({ name: 'abc', status: true, optional: 1 });
const doc = await obj.save();

doc.set({ optional: undefined });

console.log('doc.isModified()', doc.isModified());
console.log('doc.optional', doc.optional);

await doc.save();

process.exit();
doc.isModified() false
doc.optional 1

Expected Behavior

doc.isModified() true
doc.optional undefined
@vkarpov15 vkarpov15 added has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue 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 Jan 25, 2025
@vkarpov15 vkarpov15 added this to the 8.9.6 milestone Jan 27, 2025
vkarpov15 added a commit that referenced this issue Jan 30, 2025
fix(document): allow setting values to undefined with set(obj) syntax with strict: false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
2 participants