mongoose icon indicating copy to clipboard operation
mongoose copied to clipboard

dot notation does not work on Mixed types when creating new document

Open jessefulton opened this issue 12 years ago • 3 comments

When trying to create a new document with nested properties using dot notation, it fails if the nested property is within a "Mixed" type. Dot notation works with more strict nested definitions, I'd expect it to work with mixed as well? I added the following tests to model.create.test.js:

This one passes (setting mix.val using a nested object):


    it('works when explicitly generating mixed obj', function(done){
      var schema2 = Schema({ name:String, mix: { type: Schema.Types.Mixed }, nested: {prop: String} });
      var M = db.model('model-create-M', schema2, 'model-create-'+random());
      var m1 = new M({"name": "test", "mix": {"val": "foo"}, "nested.prop": "bar"});
      assert.equal("test", m1.name);
      assert.equal("foo", m1.mix.val);
      assert.equal("bar", m1.nested.prop);
      m1.save(function(err, doc) {
        if (err) {
          return done(err);
        }
        assert.equal("test", doc.name);
        assert.equal("foo", doc.mix.val);
        done(err);
      });
    })

And the following fails (setting mix.val using dot notation):

    it('works when passing dot notation to mixed property', function(done){
      var schema2 = Schema({ name:String, mix: { type: Schema.Types.Mixed }, nested: {prop: String} });
      var M = db.model('model-create-M', schema2, 'model-create-'+random());
      var m1 = new M({"name": "test", "mix.val": "foo", "nested.prop": "bar"});
      assert.equal("test", m1.name);
      assert.equal("foo", m1.mix.val);
      assert.equal("bar", m1.nested.prop);
      m1.save(function(err, doc) {
        if (err) {
          return done(err);
        }
        assert.equal("test", doc.name);
        assert.equal("foo", doc.mix.val);
        done(err);
      });
    })

jessefulton avatar Mar 04 '14 22:03 jessefulton

in schema the property val is not defined in mix, therefore the dot notation doesn't work, whereas mix can handle an object as value.

mongoose handles this correctly.

sharkboy1976 avatar Mar 11 '14 14:03 sharkboy1976

I guess this is connected with #11253

Uzlopak avatar Jan 24 '22 04:01 Uzlopak

I confirmed that OP's 2nd test passes in Mongoose 6.x, so we can close this

vkarpov15 avatar Mar 26 '22 18:03 vkarpov15