Search
 
SCRIPT & CODE EXAMPLE
 

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);
Comment

what is schemas mongoose

module.exports = mongoose.model('Email', emailSchema)
Comment

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);
Comment

what is schemas mongoose

let mongoose = require('mongoose')

let emailSchema = new mongoose.Schema({
  email: String
})

module.exports = mongoose.model('Email', emailSchema)
Comment

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
    }
  });
Comment

schema mongoose

const fruitSchema = new mongoose.Schema ({
  name: {
    type: String
  },
  rating: {
    type: Number,
    min: 1,
    max: 10
  },
  review: String
});
Comment

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);
Comment

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
    }
  });
Comment

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;
Comment

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 })
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript switch syntax 
Javascript :: if else react render in jsc 
Javascript :: promise catch javascript 
Javascript :: leaflet geojson style stroke width 
Javascript :: CodePen Home Load iframe on click 
Javascript :: bogo sort js 
Javascript :: EXPRESS APP REGISTER HANDLEBARS VIEW ENGINE 
Javascript :: es6 arrow function 
Javascript :: .env file example react native 
Javascript :: some js es6 
Javascript :: sort array of objects based on another array javascript 
Javascript :: Angular Quick Tip: Binding Specific Keys to the Keyup and Keydown Events 
Javascript :: associative multidimensional array javascript 
Javascript :: ckeditor 5 on blur 
Javascript :: number of edges between set of nodes networkx 
Javascript :: kaboom.js 
Javascript :: javascript create a multidimensional array 
Javascript :: how to create an async function from a string in node js 
Javascript :: print json object 
Javascript :: delete value from json array with index 
Javascript :: star looping javascript 
Javascript :: includes in javascript 
Javascript :: objeto con método javascript 
Javascript :: download string as file express js 
Javascript :: get date in milliseconds javascript 
Javascript :: async arrow function javascript 
Javascript :: assign freemarker expressions to variables 
Javascript :: js get data url of pdf 
Javascript :: how to check popup is open or not in javascript 
Javascript :: how to access items inside anonymous object 
ADD CONTENT
Topic
Content
Source link
Name
4+1 =