Full-Stack Web Development with Vue.js and Node

(singke) #1
Introducing MongoDB Chapter 3

This creates a connection with our Mongoose database. Now, let's run the app with the
following command:


$ nodemon app.js

and displays a message in our Terminal if it succeeded or failed:


That's it! We have successfully made a connection to our MongoDB database. The URL here


is the locally hosted database URL.


Creating records in Mongoose


Let's start by creating a new model in our application's express_app. Create a folder called


models in the root of the project and name it User.js.


We are using a capital letter for the starting letter of the file name. Also,
we are using the single form for models. Contrary to this, for
controllers, we use the plural form and lowercase letters, such
as users.js.

Once we create the file, paste the following code into it:


const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const UserSchema = new Schema({
name: String,
email: String
})

const User = mongoose.model("User", UserSchema)
module.exports = User
Free download pdf