admin管理员组

文章数量:1417469

Is there a specific setting you need to setup in order for mongoose to have this below Model working?

// The Model

let theModelSchema = new Schema({
  created_on: {
    // type: Date, // Either Date or Number
    type: Number,
    default: Date.now(), // have also tried Date.now
  },
})

let TheModel = mongoose.model('TheModel', theModelSchema)

// Save first entry

let newEntry = new TheModel({
  value: 'randomValues here'
})

// The default date will be for example 123 here
newEntry.save(function(err, savedNewEntry){
  console.log(savedNewEntry.created_on) // 123
})

// Save second entry a little bit later

newEntry = new TheModel({
  value: 'randomValues here'
})

// The default date should be 125(or whatever)
// But for some reason it stays as 123 and only updates // // when the server restats
newEntry.save(function(err, savedNewEntry){
  console.log(savedNewEntry.created_on) // still 123, should be different
})

Is there a specific setting you need to setup in order for mongoose to have this below Model working?

// The Model

let theModelSchema = new Schema({
  created_on: {
    // type: Date, // Either Date or Number
    type: Number,
    default: Date.now(), // have also tried Date.now
  },
})

let TheModel = mongoose.model('TheModel', theModelSchema)

// Save first entry

let newEntry = new TheModel({
  value: 'randomValues here'
})

// The default date will be for example 123 here
newEntry.save(function(err, savedNewEntry){
  console.log(savedNewEntry.created_on) // 123
})

// Save second entry a little bit later

newEntry = new TheModel({
  value: 'randomValues here'
})

// The default date should be 125(or whatever)
// But for some reason it stays as 123 and only updates // // when the server restats
newEntry.save(function(err, savedNewEntry){
  console.log(savedNewEntry.created_on) // still 123, should be different
})

It works for me when I run it the first time, but it then saves another entry, it doesn't update the time in the new entry, it keeps the old time, as long as the same server instance is running.

I have tried: delete newEntry after the code runs

So it seems that the server chaches the new Date and uses it on every entry The mongoose documentation tells me I am doing this right, as well as many other posts. http://mongoosejs./docs/defaults.html

So what am I doing wrong here? I of course know of many workarounds, but you want your model to have this automated. Help is greatly appreciated, thanks in advance!

Share Improve this question asked Jan 26, 2018 at 12:16 Mark OwuyaMark Owuya 712 silver badges5 bronze badges 1
  • it will not update the same doc, insted it will create new doc with the new Date. – Saikat Chakrabortty Commented Jan 26, 2018 at 12:22
Add a ment  | 

1 Answer 1

Reset to default 5

Ok, kind of lame to respond to your own question...this is weird but it started working now.

I updated mongoose (I don't know if this had anything to do with it)

I also removed the: delete newEntry So that seemed to just be redundant

However, this seem to have worked this time around

created_on: {
  type: Number,
  // Changing Date.now() to Date.now did the trick this time around
  default: Date.now, 
},

So I only changed Date.now() to Date.now, which I by the way had already tried before...so maybe the mongoose update made a difference? If anyone knows why this happened, please let me know.

Thanks

本文标签: javascriptMongoose Default DateStack Overflow