Setting up User 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 293:- Setting up User Schema

To set up a user schema in MongoDB, we can use Mongoose, which is a MongoDB object modeling tool designed to work in an asynchronous environment. Here are the steps to set up a user schema:

  1. Install Mongoose: To install Mongoose, run the following command in your terminal:

Copy code

npm install mongoose

  1. Create a new file: Create a new file called user.js in your models folder.
  2. Import Mongoose: To use Mongoose, we need to import it into our user.js file:

javascriptCopy code

const mongoose = require('mongoose');

  1. Define the user schema: We can define the user schema using the Schema function provided by Mongoose:

javascriptCopy code

const userSchema = new mongoose.Schema({  name: { type: String, required: true },  email: { type: String, required: true, unique: true },  password: { type: String, required: true }, });

This defines a schema with three fields: name, email, and password. The required property ensures that each field is mandatory, while the unique property ensures that the email field is unique for each user.

  1. Export the schema: We need to export the schema so that it can be used in other parts of our application:

javascriptCopy code

module.exports = mongoose.model('User', userSchema);

This creates a model named User based on the userSchema that we defined earlier.

With these steps, we have successfully set up a user schema in MongoDB using Mongoose. We can now use this schema to create, read, update, and delete user data in our application.

34. Manual Authentication

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