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 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
  }
});
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 :: use localstorage react hook 
Javascript :: convert string with dot or comma as decimal separator to number in javascript 
Javascript :: JSE Data 
Javascript :: usestate hook with callback 
Javascript :: angular async 
Javascript :: svg clientx 
Javascript :: Array iteration in ES6 
Javascript :: javaScript getMonth() Method 
Javascript :: joi.validate is not a function stack overflow 
Javascript :: flutter post request 
Javascript :: js get html title 
Javascript :: jquery parse html 
Javascript :: pagination react 
Javascript :: simple nodejs server 
Javascript :: how to change object property value in javascript 
Javascript :: global scope js 
Javascript :: javascript split a string 
Javascript :: openstreetmap api example javascript 
Javascript :: how to use promise.all 
Javascript :: js replace whole word and not words within words 
Javascript :: common javascript coding interview questions 
Javascript :: filter buttons react 
Javascript :: js export options 
Javascript :: javascript copy clipboard 
Javascript :: dynamic for loop react 
Javascript :: empty object is falsy 
Javascript :: reverse string javascript 
Javascript :: spread and rest javascript 
Javascript :: multiple replace 
Javascript :: unknown provider angularjs 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =