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

Comments: 2

profile
@niteshguptav63
17-Nov-2024, 01:39 PM

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

profile
@niteshguptav63
16-Nov-2024, 10:56 AM

When will I get my course?

profile
@admin79
17-Nov-2024, 01:29 PM

Now, Your query was resolved.

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?