胡子拉碴的豆腐 · Mongoose URI is ...· 3 周前 · |
失望的斑马 · 总结sencha cmd build ...· 4 月前 · |
热心的大熊猫 · 苏州可以坐地铁去上海了,杭州离这个小目标还有多远?· 4 月前 · |
傻傻的吐司 · wpf mvvm 控制隐藏 - CSDN文库· 4 月前 · |
酷酷的打火机 · 关于印发《上海市科技成果转化创新改革试点实施 ...· 6 月前 · |
追风的牛肉面 · 深度学习:数据驱动的人工智能未来-百度开发者中心· 7 月前 · |
A Mongoose connection is a Node.js module that establishes and manages connections between a Node.js application and a MongoDB database. It optimizes resource utilization, handles connection pooling, and manages errors, facilitating efficient data operations.
npm install mongoose
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/mydatabase', {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
console.log('Connected to MongoDB');
})
.catch((error) => {
console.error('Error connecting to MongoDB:', error);
});
const db = mongoose.connection;
db.on('error', (error) => {
console.error('MongoDB connection error:', error);
});
db.once('open', () => {
console.log('Connected to MongoDB');
});
db.on('disconnected', () => {
console.log('Disconnected from MongoDB');
});
process.on('SIGINT', () => {
mongoose.connection.close(() => {
console.log('Mongoose connection is disconnected due to application termination');
process.exit(0);
});
});
Connection Events in mongoose: In Mongoose, connection events play an important role in monitoring and controlling the interaction between your Node.js application and MongoDB databases. Key connection events include:
console.error(
'Error connecting to MongoDB:'
, error.message);
// Handle specific error conditions
if
(error.name ===
'MongoNetworkError'
) {
console.error(
'Network error occurred. Check your MongoDB server.'
);
}
else
if
(error.name ===
'MongooseServerSelectionError'
) {
console.error(
'Server selection error. Ensure'
+
' MongoDB is running and accessible.'
);
}
else
{
// Handle other types of errors
console.error(
'An unexpected error occurred:'
, error);
// Handling connection events
const db = mongoose.connection;
db.on(
'error'
, (error) => {
console.error(
'MongoDB connection error:'
, error);
db.once(
'open'
, () => {
console.log(
'Connected to MongoDB'
);
db.on(
'disconnected'
, () => {
console.log(
'Disconnected from MongoDB'
);
// Gracefully close the connection when the application exits
process.on(
'SIGINT'
, () => {
mongoose.connection.close(() => {
console.log(
'Mongoose connection is disconnected'
+
' due to application termination'
);
process.exit(0);
To open a connection to your MongoDB database using Mongoose, you typically do this at the beginning of your Node.js application. You need to specify the MongoDB URI , which includes information about the database server and authentication details.
const mongoose = require('mongoose');
// MongoDB URI
const dbURI = 'mongodb://localhost/mydatabase';
// Connect to MongoDB
mongoose.connect(dbURI, { useNewUrlParser: true, useUnifiedTopology: true });
// Event handling for successful connection
mongoose.connection.on('connected', () => {
console.log('Mongoose connected to ' + dbURI);
});
// Event handling for connection error
mongoose.connection.on('error', (err) => {
console.error('Mongoose connection error: ' + err);
});
// Event handling when the connection is disconnected
mongoose.connection.on('disconnected', () => {
console.log('Mongoose disconnected');
});
Closing the connection is important when your Node.js application is shutting down or when the database connection is no longer needed. You can use mongoose.connection.close() to close the connection.
// Close the Mongoose connection when the Node.js process exitsLike Article
process.on('SIGINT', () => {
mongoose.connection.close(() => {
console.log('Mongoose connection closed through app termination');
process.exit(0);
});
});
// Or you can close the connection
//explicitly when you're done with it
// mongoose.connection.close();
热心的大熊猫 · 苏州可以坐地铁去上海了,杭州离这个小目标还有多远? 4 月前 |
傻傻的吐司 · wpf mvvm 控制隐藏 - CSDN文库 4 月前 |
追风的牛肉面 · 深度学习:数据驱动的人工智能未来-百度开发者中心 7 月前 |