0

I have a basic web app that I can add a user, update a user, find a user and delete a user. I'm doing this on Postman using Post, Get, and Delete.

I'm trying to add a field to my MongoDB (through Mongoose) that will automatically insert the current date when I create a new user. I've tried a few different methods from questions I've found on this site, but none seem to be working. This is the schema for the db. Thanks

var mongoose = require('mongoose');

var userSchema = mongoose.Schema({

name: String,
times:{
        description:String
      }

});

module.exports = mongoose.model('User', userSchema);
2

1 Answer 1

0

Schemas have a few configurable options which can be passed to the constructor or set directly. If set timestamps, mongoose assigns createdAt and updatedAt fields to your schema, the type assigned is Date. By default, the name of two fields are createdAt and updatedAt, custom the field name by setting timestamps.createdAt and timestamps.updatedAt.

var mongoose = require('mongoose');

var userSchema = mongoose.Schema({
  name: String,
  times: {
    description: String
  }
}, {timestamps: {createdAt: 'created_at'}});

module.exports = mongoose.model('User', userSchema);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.