Skip to content

Value is not saved in the database. #1598

Description

@hokaccha

When I modify value of date field, and call save method, then model object's value is modified, but value isn't saved in database.

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/tests');

var Model = new mongoose.Schema({ date : Date });
var M = mongoose.model('Model', Model);

function log(date) {
  return console.log(date.getFullYear() + '-'
                    + (date.getMonth() + 1) + '-'
                    + date.getDate());
}

M.create({ date: new Date('2013-01-01') }, function(err, m) {
  log(m.date); //=> 2013-1-1

  var date = m.date;
  date.setMonth(date.getMonth() + 1);
  m.date = date;

  // month is modified
  log(m.date); //=> 2013-2-1

  m.save(function(err, m2) {
    // month is modified
    log(m2.date); //=> 2013-02-01

    M.findById(m2._id, function(err, m3) {
      // month is **not** modified
      log(m3.date); //=> 2013-01-01

      process.exit();
    });
  });
});

I think the value that is passed to save method should always be equal to the value of database.

m.save(function(err, m2) {
  M.findById(m2._id, function(err, m3) {
    expect(m2).to.eql(m3);
  });
});

versions:

  • mongoose: 3.6.15
  • mongodb: 2.0.6

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsThis issue is due to a mistake or omission in the mongoosejs.com documentation

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions