connect2mongodb v1.1.24
Installation:-
npm i connect2mongodb
Warning:-
Make sure you must have installed exact same version i.e.:- mongoose@8.3.4, in NextJS/Express Server.
npm i mongoose@8.3.4
Otherwise it will connect to the DB, but starts giving error while trying to access the DB.
How to use:-
Method 1:- With A Fixed Database:-
Firstly update your .env:-
Your MONGODB_URI will look like this:- mongodb+srv://username:password@hostname/your_database_name
MONGODB_URI=mongodb+srv://username:password@hostname/your_database_name
This module will atuomatically fetch your MONGODB_URI from the .env, but make sure to use the same/default format provided by MongoDB, and make sure to add the database name too.
Import the module:-
import { connect2MongoDB, disconnect2MongoDB } from 'connect2mongodb'
To Connect:-
await connect2MongoDB();
To Disconnect:-
await disconnect2MongoDB();
To Connect with a return value:-
This will return true if Connected to the database else false.
const customVariable = await connect2MongoDB();
console.log(customVariable);
To Disconnect with a return value:-
This will return true after Disconnecting to the database.
const customVariable = await disconnect2MongoDB()
console.log(customVariable);
Method 2:- With a dynamic database:-
Firstly update your .env:-
Your MONGO_URI will look like this:- mongodb+srv://username:password@host_name/
MONGO_URI=mongodb+srv://username:password@hostname/
This module will atuomatically fetch your MONGO_URI from the .env, but make sure to use the same format, and don't add the database name after last '/'.
Import the module:-
import { connect2MongoDB, disconnect2MongoDB } from 'connect2mongodb'
To Connect:-
await connect2MongoDB('Your Database Name');
To Disconnect:-
await disconnect2MongoDB();
To Connect with a return value:-
This will return true if Connected to the database else false.
const customVariable = await connect2MongoDB('Your Database Name');
console.log(customVariable);
To Disconnect with a return value:-
This will return true after Disconnecting to the database.
const customVariable = await disconnect2MongoDB()
console.log(customVariable);
23 hours ago
9 days ago
22 days ago
1 month ago
1 month ago
2 months ago
2 months ago
2 months ago
2 months ago
3 months ago
3 months ago
3 months ago
3 months ago
4 months ago
4 months ago
4 months ago
4 months ago
5 months ago
7 months ago
7 months ago
5 months ago
6 months ago
6 months ago
6 months ago
6 months ago
8 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
8 months ago
8 months ago
7 months ago
8 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago