First of all you should have installed MongoDB and be able to run it before you install mongoose.

In order to install mongoose, we’ll use npm command in the project folder. But first, you should go to the relevant project folder in command line.

Hint: You can use cd command to change directories in command line. (ex: cd Desktop)

  1. Install mongoose with npm
npm i mongoose

Once that’s done, we can go over to the next step which is requiring mongoose in our project. Whether you use Atom, VScode or any other IDE, go to the actual javascript file which is usually called as app.js. Then assign mongoose constant with require.

2. Require mongoose in app.js

const mongoose = require("mongoose");

Then, we should connect mongoose with MongoDB.

3. Connect to MongoDB

mongoose.connect("mongodb://localhost:27017/dogsDB");

The url is default until the last slash. Then you should give your database name. dogsDB is the name of the database in this example. Even if you haven’t one yet, it will create a new database.

Try to run your code in command line(ex: node app.js). If you got any deprecation warning, you can pass the recommendation as a parameter to connect function.

#mongoose #database #mongodb #nosql #nodejs

How to Install and use Mongoose with Node.js for MongoDB
18.20 GEEK