mongoose Getting started with mongoose Installation

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

Installing mongoose is as easy as running the npm command

npm install mongoose --save

But make sure you have also installed MongoDB for your OS or Have access to a MongoDB database.


Connecting to MongoDB database:

1. Import mongoose into the app:

import mongoose from 'mongoose';

2. Specify a Promise library:

mongoose.Promise = global.Promise;

3. Connect to MongoDB:

mongoose.connect('mongodb://127.0.0.1:27017/database');

/* Mongoose connection format looks something like this */
mongoose.connect('mongodb://USERNAME:PASSWORD@HOST::PORT/DATABASE_NAME');

Note:

  • By default mongoose connects to MongoDB at port 27017, Which is the default port used by MongoDB.

  • To connect to MongoDB hosted somewhere else, use the second syntax. Enter MongoDB username, password, host, port and database name.

MongoDB port is 27017 by default; use your app name as the db name.



Got any mongoose Question?