query with selected fields #891

Closed
kof opened this Issue May 3, 2012 · 8 comments

Projects

None yet

2 participants

@kof
kof commented May 3, 2012

If I run a query with selected fields, unselected fields still exist on the document object (getters ?). This causes schema validation issues.

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var assert = require('assert')
mongoose.connect('localhost', 'testing_findone');
var schema = new Schema({
  name: {
    type: String,
    required: true,
    validate: [function(str) {
        return str.length < 20;
    }, 'MaxLength']
  },
  nick: {
    type: String,
    required: true
  },
  soemthing: String
}, {strict: true});

var A = mongoose.model('A', schema);

mongoose.connection.on('open', function () {
  var a = new A({
    name: 'name',
    nick: 'nick'
  });
  a.save(function(err) {
    assert.equal(null, err);

    A.findOne({},['nick'], function (err, doc) {
      if (err) console.error(err.stack||err);
      console.log('"name" in doc should be false but it is - ', 'name' in doc);

      doc.nick = 'blubb';
      doc.save(function(err) {
        console.error(err);
        mongoose.connection.db.dropDatabase(function () {
          mongoose.connection.close();
        });
      });
    });

  });
});
@aheckmann
Collaborator

which mongoose version?

@kof
kof commented May 3, 2012

just tried with 2.6.2, also 2.5.7 had this issue as I think.

@aheckmann
Collaborator

this is expected behavior. name in doc is true b/c there exists a special getter/setter object that implements all the internals which is compiled into the base class from the schema paths. this is core to how mongoose has always worked. mongoose documents are not vanilla js objects and as such bring along with them some oddities.

@kof
kof commented May 4, 2012

ok then you need to go away from this check, I think I saw it somewere in code "propname in doc" to detect if the property was changed, instead you need to save a list of changed properties using your getters and validate/save only fields which are changed.

@aheckmann
Collaborator

we do. where is the invalid check being used?

@kof
kof commented May 4, 2012

if you run the code above, you will get an error inside of #save callback, however this should not happen. I am selecting just one field, I am changing just this field, why is mongoose calling validator on a field I haven't changed and haven't selected?

@aheckmann
Collaborator

ah yeah thats pointless isn't it. good catch.

@aheckmann
Collaborator

to be clear, re: name in doc, I want this to work as you expect too but it will take a rewrite of docs/schema compilation I think.

@aheckmann aheckmann added a commit that closed this issue Jul 9, 2012
@aheckmann aheckmann stop needless validation
closes #891

If the field was not selected or changed we should bother
validating it.
5656142
@aheckmann aheckmann closed this in 5656142 Jul 9, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment