This repository contains the code for a workshop on Mongoose and Express JS. The workshop covers the following topics:
To get started with the workshop, follow these steps:
-
Clone this repository to your local machine. Open your terminal and run the following command:
- Link to Donload Git: Git
git clone https://github.com/adithyapaib/mongocec
- Configure the git user name and email using the following commands:
git config --global user.name "Your Name" git config --global user.email "adithy@email.com"
Install MongoDB on your local machine. You can download the installer from the following link: MongoDB
-
Navigate to the project directory:
cd mongocec
-
Install the required dependencies:
npm install
-
Install Nodemon globally:
npm install -g nodemon
-
Start the application:
nodemon index.js
-
Open your browser and navigate to
http://localhost:3000/
to see the application in action.
- index.js
const mongoose = require('mongoose'); // Mongoose for MongoDB interactions
const DATABASE = `cec`;
const COLLECTION = `users`;
// Connecting to MongoDB
// The database is located at localhost:27017 and the database name is 'test'
mongoose.connect('mongodb://localhost:27017/cec').then(() => console.log('Connected to MongoDB'));
// Defining a schema for the 'User' model
// A user has a 'name' and an 'age', both fields are required
const userSchema = new mongoose.Schema({
name: String,
age: Number,
email: String
});
// Creating the 'User' model using the 'userSchema'
// The collection in the MongoDB database will be 'test'
const User = mongoose.model(COLLECTION, userSchema);
//1. CREATE OPERATION
// Creating a new user using Create method
User.create({ name: 'John Doe', age: 25 }).then((user) => console.log(user))
// Creating a new user using Save method
const adithya = new User({ name: 'Adithya',age: 22});
adithya.save()
- Create a new Collection activity in the MongoDB database
- Create a new Schema for the activity
- Create a new Model for the activity
- Create a new activity using the Create method
This project is licensed under the MIT license. Please see the LICENSE file for more information.