1const userSchema = mongoose.Schema(
2 {
3 email: String,
4 },
5 { timestamps: true }
6);
7
8const User = mongoose.model("User", userSchema);
9
10const doc = await User.create({ email: "test@google.com" });
11
12doc.createdAt; // 2020-07-06T20:36:59.414Z
13doc.updatedAt; // 2020-07-06T20:36:59.414Z
14
15doc.createdAt instanceof Date; // true
1const mongoose = require('mongoose');
2
3const UserSchema = new mongoose.Schema({
4 name: {
5 type: String,
6 required: true
7 }
8}, {
9 timestamps: true
10})
11
12const User = mongoose.model('User', UserSchema);
13
14module.exports = User;
1 var mongoose = require('mongoose');
2 var Schema = mongoose.Schema;
3
4 var blogSchema = new Schema({
5 title: String, // String is shorthand for {type: String}
6 author: String,
7 body: String,
8 comments: [{ body: String, date: Date }],
9 date: { type: Date, default: Date.now },
10 hidden: Boolean,
11 meta: {
12 votes: Number,
13 favs: Number
14 }
15 });