I read the quick start from the Mongoose website and I almost copy the code, but I cannot connect the MongoDB using Node.js.
var mongoose = require(\'mongoos
Mongoose's default connection logic is deprecated as of 4.11.0. It is recommended to use the new connection logic:
Here is the example from npm module: mongoose-connect-db
// Connection options
const defaultOptions = {
// Use native promises (in driver)
promiseLibrary: global.Promise,
useMongoClient: true,
// Write concern (Journal Acknowledged)
w: 1,
j: true
};
function connect (mongoose, dbURI, options = {}) {
// Merge options with defaults
const driverOptions = Object.assign(defaultOptions, options);
// Use Promise from options (mongoose)
mongoose.Promise = driverOptions.promiseLibrary;
// Connect
mongoose.connect(dbURI, driverOptions);
// If the Node process ends, close the Mongoose connection
process.on('SIGINT', () => {
mongoose.connection.close(() => {
process.exit(0);
});
});
return mongoose.connection;
}