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

Setting map on update with an Object throws an Error #10485

Closed
foldess opened this issue Jul 23, 2021 · 2 comments
Closed

Setting map on update with an Object throws an Error #10485

foldess opened this issue Jul 23, 2021 · 2 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@foldess
Copy link

foldess commented Jul 23, 2021

Do you want to request a feature or report a bug?
Bug
What is the current behavior?
Creating an schema like:

const child = new mongoose.Schema(
    {
        name: {
            type: mongoose.Schema.Types.Map,
            of: String,
        }
    }
)

const parent = new mongoose.Schema(
    {
        children: {
            type: [child],
        }
    }
)
mongoose.model('Parent', parent)

And than trying to do an update of a single child like results in error

updateData = { name: {en: "en Name", it: "it Name"} }

Parent.findOneAndUpdate({ 'children._id': id }, { 'children.$.name': updateData.name }, { new: true })

OR

Parent.findOneAndUpdate({ 'children._id': id }, { $set { 'children.$.name': updateData.name } }, { new: true })

gives the following error:

Cast to string failed for value \"{ it: 'it Name', en: 'en Name' }\" (type Object) at path \"name.$*\"

Of course in my case parent an child have more attributes, and i want to update only the attributes that are passed that are present in the update data however if i just try to set the whole 'children.$' as updateData i lose already present data because i'm overwriting the whole sub Document

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

What is the expected behavior?
Update the sub Document wihout overwriting it as a whole

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
node: 14.16.0
mongoose: 5.12.10
mongodb: 4.4.6

@foldess foldess changed the title Setting map on update with and Object throws an Error Setting map on update with an Object throws an Error Jul 23, 2021
@foldess
Copy link
Author

foldess commented Jul 26, 2021

I wrote a temporary solution for this matter while i wait a response

let object = Object.keys(updateData).reduce((obj, item) => {
            if (typeof updateData[item] === 'object' && !Array.isArray(updateData[item])) {
                const key = 'children.$.' + item
                 Object.keys(updateData[item]).forEach((item2) => {
                    obj[key + '.' + item2] = updateData[item][item2]
                })
                return obj
            } else {
                obj['children.$.' + item] = updateData[item]
                return obj
            }
        }, {})

const update = await Parent.findOneAndUpdate({ 'children._id': id }, { ...object }, { new: true })

@IslandRhythms
Copy link
Collaborator

const mongoose = require('mongoose');

const child = new mongoose.Schema(
    {
        name: {
            type: mongoose.Schema.Types.Map,
            of: String,
        }
    }
)

const parent = new mongoose.Schema(
    {
        children: {
            type: [child],
        }
    }
)
const Parent = mongoose.model('Parent', parent)



async function run() {
    await mongoose.connect('mongodb://localhost:27017/test', {
        useNewUrlParser: true,
        useUnifiedTopology: true,
      });
    
    await mongoose.connection.dropDatabase();

   let entry = await Parent.create({
        children: [{name: {github: 'hello', twitter: 'world'}}]
    });
    console.log(entry);
    let newEntry = await Parent.findOneAndUpdate({ _id: entry._id }, { $set: { 'children.$.name': { fart:'boy' }} });
    console.log(newEntry.children[0]);
}
run();

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Jul 27, 2021
@vkarpov15 vkarpov15 added this to the 5.13.4 milestone Jul 28, 2021
vkarpov15 added a commit that referenced this issue Jul 28, 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