JAVASCRIPT
mongoose schema
/// model/User.js
const mongoose = require("mongoose");
const UserScheme = mongoose.Schema({
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
unique: true,
},
password: {
type: String,
required: true,
},
date: {
type: Date,
default: Date.now(),
},
});
module.exports = mongoose.model("user", UserScheme);
what is schemas mongoose
module.exports = mongoose.model('Email', emailSchema)
mongoose schema
const mongoose = require("mongoose");
const { stringify } = require("querystring");
const { Schema } = mongoose;
const blogSchema = new Schema({
title: String,
metaDes: String,
des: stringify,
});
module.exports = mongoose.model("posts", blogSchema);
what is schemas mongoose
let mongoose = require('mongoose')
let emailSchema = new mongoose.Schema({
email: String
})
module.exports = mongoose.model('Email', emailSchema)
Defining Schema mongoose
import mongoose from 'mongoose';
const { Schema } = mongoose;
const blogSchema = new Schema({
title: String, // String is shorthand for {type: String}
author: String,
body: String,
comments: [{ body: String, date: Date }],
Javascript
1
import mongoose from 'mongoose';
2
const { Schema } = mongoose;
3
date: { type: Date, default: Date.now },
hidden: Boolean,
meta: {
votes: Number,
favs: Number
}
});
schema mongoose
const fruitSchema = new mongoose.Schema ({
name: {
type: String
},
rating: {
type: Number,
min: 1,
max: 10
},
review: String
});
schema in mongoose
//1st style
var mongoose = require('mongoose');
var Schemax = mongoose.Schema;
var blogSchema = new Schemax({
title: String, // String is shorthand for {type: String}
author: String,
body: String,
comments: [{ body: String, date: Date }],
date: { type: Date, default: Date.now },
hidden: Boolean,
meta: {
votes: Number,
favs: Number
}
});
module.exports = mongoose.model( 'model name' , blogSchema);
//2nd style
const mongoose = require('mongoose')
const Schema Name = mongoose.Schema({
name : {
type : String,
default : 'default txt',
},
})
module.exports = mongoose.model( 'model name' , Schema Name);
mongoose schema
import mongoose from 'mongoose';
const { Schema } = mongoose;
const blogSchema = new Schema({
title: String, // String is shorthand for {type: String}
author: String,
body: String,
comments: [{ body: String, date: Date }],
date: { type: Date, default: Date.now },
hidden: Boolean,
meta: {
votes: Number,
favs: Number
}
});
mongoose schema
import mongoose from 'mongoose';
const orderSchema = mongoose.Schema(
{
user: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'User',
},
orderItems: [
{
name: { type: String, required: true },
qty: { type: Number, required: true },
image: { type: String, required: true },
price: { type: Number, required: true },
product: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'Product',
},
},
],
shippingAddress: {
address: { type: String, required: true },
city: { type: String, required: true },
postalCode: { type: String, required: true },
country: { type: String, required: true },
},
paymentMethod: {
type: String,
required: true,
},
paymentResult: {
id: { type: String },
status: { type: String },
update_time: { type: String },
email_address: { type: String },
},
taxPrice: {
type: Number,
required: true,
default: 0.0,
},
shippingPrice: {
type: Number,
required: true,
default: 0.0,
},
totalPrice: {
type: Number,
required: true,
default: 0.0,
},
isPaid: {
type: Boolean,
required: true,
default: false,
},
paidAt: {
type: Date,
},
isDelivered: {
type: Boolean,
required: true,
default: false,
},
deliveredAt: {
type: Date,
},
},
{
timestamps: true,
}
);
const Order = mongoose.model('Order', orderSchema);
export default Order;
mongoose schema example
import mongoose from 'mongoose';
const { Schema } = mongoose;
const blogSchema = new Schema({
title: String, // String is shorthand for {type: String}
author: String,
body: String,
comments: [{ body: String, date: Date }],
date: { type: Date, default: Date.now },
hidden: Boolean,
meta: {
votes: Number,
favs: Number
}
});
mongoose schema
const productSchema = new Schema({
name: { type: String, required: true },
price: { type: String, required: true },
size: { type: String, required: true },
image: {
type: String, required: true, get(image) {
// http://localhost:5000/upload/imagename.png
return `${APP_URL}/${image}`
}
},
}, { timestamps: true, toJSON: { getters: true }, id: false })