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.findOneAndUpdate() is updating nested fields with 'strict:false' #13434

Closed
vkarpov15 opened this issue May 24, 2023 Discussed in #13432 · 1 comment · Fixed by #13450
Closed

Model.findOneAndUpdate() is updating nested fields with 'strict:false' #13434

vkarpov15 opened this issue May 24, 2023 Discussed in #13432 · 1 comment · Fixed by #13450
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@vkarpov15
Copy link
Collaborator

Discussed in #13432

Originally posted by amitpriyankar May 24, 2023
I have observed an unusual behavior when using Model.findOneAndUpdate with a collection that has a flexible schema (strict: false). When I attempt to insert a document that includes a key containing a dot (.), the behavior differs based on whether I define the type of the key in the schema. If I specify the type, the key is treated as a regular string. However, if I don't specify the type, nested fields are created in the document.

Please refer the following code:

const mongoose = require('mongoose');

const url = '<connection_url>;

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

async function main() {
    await mongoose.connect(url);

    const personSchema = new mongoose.Schema(
        {
            name: String,
            // info: Object,
        },
        {strict: false}
    );

    personSchema.index({name: 1}, {unique: true});

    const Person = mongoose.model('Person', personSchema);

    const savedDoc = await Person.findOneAndUpdate(
        {},
        {name: 'Tommy1', info: {'second.name': 'Shelby'}},
        {new: true, upsert: true}
    )
        .lean()
        .exec();

    console.log('Saved document: ', savedDoc);
    mongoose.disconnect();
}
Saved document:  {
  _id: new ObjectId("646dccd30b5d3da01e058fd0"),
  __v: 0,
  info: { second: { name: 'Shelby' } },
  name: 'Tommy1'
}

I check the doc but didn't find anything which explains this behavior. Could someone please help?

mongoose: v7.1.1
MongoDB Server: 4.0

@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label May 24, 2023
@vkarpov15 vkarpov15 added this to the 7.2.3 milestone May 24, 2023
@IslandRhythms IslandRhythms 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 24, 2023
@IslandRhythms
Copy link
Collaborator

image

const mongoose = require('mongoose');

const testSchema = new mongoose.Schema({
  name: String,
  info: Object
}, { strict: false });

const Test = mongoose.model('Test', testSchema);

async function run() {
  await mongoose.connect('mongodb://localhost:27017');
  await mongoose.connection.dropDatabase();

  const doc = await Test.findOneAndUpdate({}, { name: 'Test Testerson', info: { 'second.name': 'Quiz' } }, { new: true, upsert: true });
  console.log('doc', doc);
  mongoose.disconnect();
}
run();

@vkarpov15 vkarpov15 modified the milestones: 7.2.3, 7.2.2 May 28, 2023
vkarpov15 added a commit that referenced this issue May 30, 2023
fix(update): allow setting paths with dots under non-strict paths
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
Development

Successfully merging a pull request may close this issue.

2 participants