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

Adding a new field to a Map on nested subdocument fails with updateOne #10720

Closed
codespork opened this issue Sep 14, 2021 · 0 comments
Closed
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@codespork
Copy link

Do you want to request a feature or report a bug?
bug

What is the current behavior?

ObjectExpectedError: Tried to set nested object field `children.$*` to primitive value `1` and strict mode is set to throw.
    at SubdocumentPath.cast (/repro/node_modules/mongoose/lib/schema/SubdocumentPath.js:157:11)
    at SubdocumentPath.SchemaType.applySetters (/repro/node_modules/mongoose/lib/schematype.js:1122:12)
    at Map.set (/repro/node_modules/mongoose/lib/types/map.js:90:11)
    at Map.$__runDeferred (/repro/node_modules/mongoose/lib/types/map.js:169:12)
    at new MongooseMap (/repro/node_modules/mongoose/lib/types/map.js:29:10)
    at Map.cast (/repro/node_modules/mongoose/lib/schema/map.js:59:12)
    at Map.SchemaType.applySetters (/repro/node_modules/mongoose/lib/schematype.js:1122:12)
    at Map.SchemaType._castForQuery (/repro/node_modules/mongoose/lib/schematype.js:1553:15)
    at Map.SchemaType.castForQuery (/repro/node_modules/mongoose/lib/schematype.js:1543:15)
    at Map.SchemaType.castForQueryWrapper (/repro/node_modules/mongoose/lib/schematype.js:1520:20) {
  path: 'children.$*'
}

If the current behavior is a bug, please provide the steps to reproduce.

const mongoose = require('mongoose');
(async () => {
  try {
    const parentSchema = new mongoose.Schema({
      data: new mongoose.Schema({
        children: {
          type: Map,
          of: new mongoose.Schema({ age: Number }, { _id: false })
        },
      }, { _id: false })
    });
    const Parent = mongoose.model('Parent', parentSchema);
    await mongoose.connect('mongodb://localhost:27017/test');
    const parent = new Parent({
      data: {
        children: {
          lisa: { age: 5 },
          john: { age: 7 }
        }
      }
    });
    await parent.save();
    await Parent.updateOne({ _id: parent.id }, { $set: { "data.children.kenny": { age: 1 } } });
    // this works
    // await mongoose.connection.getClient().db().collection("parents").updateOne({ _id: parent._id }, { $set: { "data.children.kenny": { age: 1 } } });
  }
  catch (error) {
    console.log(error);
  }
})();

What is the expected behavior?

{
  "data": {
    "children": {
      "lisa": { "age": 5 },
      "john": { "age": 7 },
      "kenny": { "age": 1 }
    }
  }
}

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
nodejs = 14.15.1
mongoose = 6.0.5
mongodb (node driver) = 4.1.1
mongodb (database version) = 4.4.8

@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Sep 15, 2021
@vkarpov15 vkarpov15 added this to the 6.0.8 milestone Sep 15, 2021
@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 Sep 15, 2021
vkarpov15 added a commit that referenced this issue Sep 21, 2021
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

No branches or pull requests

3 participants