Creating the DB Schema

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 276:- Creating the DB Schema

To create a schema for a MongoDB database using Mongoose, we need to define a schema using the mongoose.Schema() method. This method takes an object that defines the schema's structure.

Here is an example of creating a simple schema for a User collection with two fields name and email:

javascriptCopy code

const mongoose = require('mongoose'); const userSchema = new mongoose.Schema({  name: {    type: String,    required: true  },  email: {    type: String,    required: true,    unique: true  } }); module.exports = mongoose.model('User', userSchema);

In the above code, we first require the mongoose module and then create a userSchema using mongoose.Schema(). Inside the schema, we define two fields, name and email, both of which are required. Additionally, we make the email field unique so that there can't be any duplicate email addresses.

Finally, we export the schema as a Mongoose model using the mongoose.model() method, passing in the name of the collection as the first argument ('User') and the schema as the second argument (userSchema).

Once we have created the schema, we can use it to perform CRUD (Create, Read, Update, Delete) operations on the corresponding collection.

31. My First Express App - Intro To Databases

2 Comments

@niteshguptav63
niteshguptav63 Nov 17, 2024 at 1:39 PM

I am not able to access videos from second class and further. I have already completed first class

@niteshguptav63
niteshguptav63 Nov 16, 2024 at 10:56 AM

When will I get my course?

@admin79
admin79 Nov 17, 2024 at 1:29 PM

Now, Your query was resolved.

Frequently Asked Questions About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support