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

Bug in Update $set in a Map property #9298

Closed
vvFiCKvv opened this issue Jul 31, 2020 · 2 comments
Closed

Bug in Update $set in a Map property #9298

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

Comments

@vvFiCKvv
Copy link

Given a schema:

  Country = new mongoose.Schema({
    cities: {
        type: Map,
        of: {
             population: {type: Number}
        }
    });

When
Country.updateOne({_id:""}, {$set: "cities.newyork.population": 10000} ).exec()
throws Mongoose maps only support string keys, got undefined

Bypassing the mongoose works:
Country.collection.update({_id:""}, {$set: "cities.newyork.population": 10000} )
But it is missing schema validation.

By changing the mongoose/lib/schema/map.js function cast = (val, doc, init) => val it's working...
But I am not sure when this should be done in order to create a pull request(maybe when $__schemaType.path ends with ".$*").

P.S I can create a pull request with a failing test in order for anyone to investigate it more.

@vkarpov15 vkarpov15 added this to the 5.9.27 milestone Aug 5, 2020
@vkarpov15 vkarpov15 added the needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue label Aug 5, 2020
@vkarpov15
Copy link
Collaborator

Which version of Mongoose are you using? On Mongoose v5.9.26, the below script succeeds.

'use strict';
  
const mongoose = require('mongoose');

mongoose.set('debug', true);
mongoose.set('useFindAndModify', false);

const { Schema } = mongoose;

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

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

  await mongoose.connection.dropDatabase();

  const CountrySchema = new mongoose.Schema({
    cities: {
        type: Map,
        of: {
             population: {type: Number}
        }
    }
  });
  const Country = mongoose.model('Country', CountrySchema);

  await Country.updateOne({}, { $set: { "cities.newyork.population": 10000 } }).exec()

  console.log('Done');
}

@vkarpov15 vkarpov15 added can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Aug 5, 2020
@vkarpov15 vkarpov15 removed this from the 5.9.27 milestone Aug 5, 2020
@vvFiCKvv
Copy link
Author

Sorry for the late delay, I was in vacations,
I did a little more investigation and it has to do with child schemas and maps.
It was difficult to pinpoint the problem exactly because our schema is complicated.
I should avoid using subschemas, for now, thank you for your involvement.

``it("should work with mongoose", async () => {

    const mongoose = require("mongoose");

    mongoose.set("debug", true);
    mongoose.set("useFindAndModify", false);


    await run();//.catch((err) => console.log(err));

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

        await mongoose.connection.dropDatabase();

        const CountrySchema = new mongoose.Schema({
            cities: {
                type: Map,
                of: {
                    population: { type: Number }
                }
            }
        },
        { strict: "throw", useNestedStrict: true, _id: false, id: false }
        );

        const CountrySuperSchema = new mongoose.Schema({
            country: CountrySchema
        });

        const Country = mongoose.model("Country", CountrySuperSchema);

        let test = await new Country();
        test.save();
        await Country.updateOne({ _id: test._id }, { $set: { "country.cities.newyork.population": 10000 } }).exec();

        console.log("Done");
    }



});

@vkarpov15 vkarpov15 reopened this Aug 28, 2020
@vkarpov15 vkarpov15 added has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Aug 28, 2020
@vkarpov15 vkarpov15 modified the milestones: 5.10.2, 5.10.3 Aug 28, 2020
@vkarpov15 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 Aug 28, 2020
vkarpov15 added a commit that referenced this issue Aug 28, 2020
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

2 participants