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}
1//Import the mongoose module
2var mongoose = require('mongoose');
3
4//Set up default mongoose connection
5var mongoDB = 'mongodb://127.0.0.1/my_database';
6mongoose.connect(mongoDB, {useNewUrlParser: true, useUnifiedTopology: true});
7
8//Get the default connection
9var db = mongoose.connection;
10
11//Bind connection to error event (to get notification of connection errors)
12db.on('error', console.error.bind(console, 'MongoDB connection error:'));
1mongoose.connect('mongodb://localhost:27017/test', {
2 useMongoClient: true,
3 connectTimeoutMS: 1000
4})
1const mongoose = require('mongoose');
2mongoose.connect('mongodb://localhost:27017/test', {useNewUrlParser: true, useUnifiedTopology: true});
3
4const Cat = mongoose.model('Cat', { name: String });
5
6const kitty = new Cat({ name: 'Zildjian' });
7kitty.save().then(() => console.log('meow'));