Value is not saved in the database. #1598

Closed
hokaccha opened this Issue Jul 29, 2013 · 6 comments

Comments

Projects
None yet
4 participants
@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
@ebensing

This comment has been minimized.

Show comment
Hide comment
@ebensing

ebensing Aug 1, 2013

Contributor

At this point in time, I do not see a simple fix for this. You will need to mark the path as modified manually.

m.markModified('date');
Contributor

ebensing commented Aug 1, 2013

At this point in time, I do not see a simple fix for this. You will need to mark the path as modified manually.

m.markModified('date');

@ebensing ebensing closed this Aug 1, 2013

@aheckmann

This comment has been minimized.

Show comment
Hide comment
@aheckmann

aheckmann Aug 2, 2013

Collaborator

The root of the issue is that the date object is being changed but mongoose does not have hooks into Date methods that tracks changes the way we do with Buffer. We should support this.

Support would just do what @ebensing suggested, markModified so you don't have to.

Collaborator

aheckmann commented Aug 2, 2013

The root of the issue is that the date object is being changed but mongoose does not have hooks into Date methods that tracks changes the way we do with Buffer. We should support this.

Support would just do what @ebensing suggested, markModified so you don't have to.

@aheckmann aheckmann reopened this Aug 2, 2013

@aheckmann

This comment has been minimized.

Show comment
Hide comment
@aheckmann

aheckmann Aug 5, 2013

Collaborator

In the meantime we should document this in the 3.6.x branch.

Collaborator

aheckmann commented Aug 5, 2013

In the meantime we should document this in the 3.6.x branch.

@aheckmann

This comment has been minimized.

Show comment
Hide comment
@aheckmann

aheckmann Sep 4, 2013

Collaborator

After some research it seems Date subclassing is not permitted by the spec/v8 to the extent necessary to add this feature. http://stackoverflow.com/questions/6075231/how-to-extend-the-javascript-date-object

Our subclass must still pass "instanceof Date" and without being able to even call the built in Date.prototype.toString on our subclass means this is a world of hurt, too much so for official support.

When modifying using Date methods such as setMonth you'll always need to call markModified manually.

Collaborator

aheckmann commented Sep 4, 2013

After some research it seems Date subclassing is not permitted by the spec/v8 to the extent necessary to add this feature. http://stackoverflow.com/questions/6075231/how-to-extend-the-javascript-date-object

Our subclass must still pass "instanceof Date" and without being able to even call the built in Date.prototype.toString on our subclass means this is a world of hurt, too much so for official support.

When modifying using Date methods such as setMonth you'll always need to call markModified manually.

@aheckmann aheckmann closed this Sep 4, 2013

@aheckmann aheckmann reopened this Sep 4, 2013

@aheckmann

This comment has been minimized.

Show comment
Hide comment
@aheckmann

aheckmann Sep 4, 2013

Collaborator

Leaving open as a docs issue.

Collaborator

aheckmann commented Sep 4, 2013

Leaving open as a docs issue.

@aheckmann aheckmann closed this in 2aed8da Sep 4, 2013

aheckmann added a commit that referenced this issue Sep 4, 2013

aheckmann added a commit that referenced this issue Sep 4, 2013

@oxbambooxo oxbambooxo referenced this issue in MongoEngine/mongoengine Nov 9, 2015

Open

ListField save Empty without modified it #1109

@aarizirf

This comment has been minimized.

Show comment
Hide comment

aarizirf commented Jul 7, 2018

asdf

@Automattic Automattic locked as resolved and limited conversation to collaborators Jul 8, 2018

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