1import mongoose from 'mongoose'
2
3export const connectDb = async () => {
4 try {
5 await mongoose.connect('mongodb://localhost:27017/test', {
6 useNewUrlParser: true,
7 useUnifiedTopology: true,
8 useCreateIndex: true,
9 })
10 } catch (error) {
11 console.log(error.message)
12 }
13}
1mongoose.connect('mongodb://localhost:27017/test', {
2 useMongoClient: true,
3 connectTimeoutMS: 1000
4})
1try {
2 mongoose.connect(MONGODB_URI || 'mongodb://localhost/YOUR_DB_NAME', {
3 useNewUrlParser: true,
4 useUnifiedTopology: true
5 }, () =>
6 console.log("connected"));
7} catch (error) {
8 console.log("could not connect");
9}
1mongoose.connect('mongodb://username:password@host:port/database?options...', {useNewUrlParser: true});