Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

mongoose update createdAt

const schema = mongoose.Schema({
  	name: String,
    createdAt: { type: Date, default: Date.now }
}, {
    timestamps: { createdAt: false, updatedAt: true }
})

const Test = mongoose.model('test', schema);

const john = await Test.create({name: 'John Doe'});
console.log(john);

// Now that will work
const updatedJohn = await Test.findOneAndUpdate({name: 'John Doe'}, { createdAt: Date.now() });
console.log(updatedJohn);
 
PREVIOUS NEXT
Tagged: #mongoose #update #createdAt
ADD COMMENT
Topic
Name
7+5 =