Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create User model #2

Open
github-learning-lab bot opened this issue Nov 27, 2020 · 0 comments
Open

Create User model #2

github-learning-lab bot opened this issue Nov 27, 2020 · 0 comments

Comments

@github-learning-lab
Copy link

Let's start by making a User model for mongodb using the mongoose library. This will be the template used to describe what each individual document will look like in our collection.

Find the "models" directory in the root of your project and open "User.js", then add this code to it:

const mongoose = require('mongoose');

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

module.exports= mongoose.model('User',UserSchema)

Looks like a JS object doesn't it? That is one of the cool things about MongoDB, it is easy to transfer data from the frontend to the backend. Think of this as a factory, or mold, that can create new User documents in a User collection.

Looking at the model above, which key (name, email, or password) needs to have unique values?

Leave a comment with your answer to continue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants