Imposing Structure to MongoDB by Mongoose Schema
Summary: Imposing structure to MongoDB by Mongoose schema.
Contents:
1. Introduction
MongoDB is a document database, noSQL database, no structure imposed on the document, which is a drawback.
Mongoose Schema can help us impose structure on documents.
📦mongodb
┗ 📂data
📦node-mongoose
┣ 📂models
┃ ┗ 📜dishes.js
┣ 📜index.js
┣ 📜package-lock.json
┣ 📜package.json
┗ 📜yarn.lock
2. Installing Mongoose
-
Create a folder named
node-mongoose
and move into the folder, typenpm init
to initialize apackage.json
file.package.json
{ "name": "node-mongoose-db", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "ycheng", "license": "ISC", "dependencies": { "mongoose": "5.1.7" } }
-
Install Mongoose by typing the following at the prompt:
npm install mongoose@5.1.7 --save
or
yarn add mongoose@5.1.7
3. Imposing structure by Mongoose Schema
-
Create a sub-folder
models
, in the sub-folder, createdishes.js
,dishes.js
const mongoose = require('mongoose'); const Schema = mongoose.Schema; const dishSchema = new Schema({ name: { type: String, required: true, unique: true }, description: { type: String, required: true } },{ timestamps: true }); var Dishes = mongoose.model('Dish', dishSchema); module.exports = Dishes;
index.js
const mongoose = require('mongoose'); const Schema = mongoose.Schema; const dishSchema = new Schema({ name: { type: String, required: true, unique: true }, description: { type: String, required: true } },{ timestamps: true }); var Dishes = mongoose.model('Dish', dishSchema); module.exports = Dishes;
Comments