Node.js 将Mongoose模式与我自己的连接(Mongo客户端)一起使用
我已使用如下入口点组织了我的服务器:Node.js 将Mongoose模式与我自己的连接(Mongo客户端)一起使用,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我已使用如下入口点组织了我的服务器: /** index.js * Here I inject the connection to the Data Access Models (DAO), * These are the classes that control the queries to MongoDB. */ const port = process.env.PORT || 3000 MongoClient.connect( { useNewUrlParser: tr
/** index.js
* Here I inject the connection to the Data Access Models (DAO),
* These are the classes that control the queries to MongoDB.
*/
const port = process.env.PORT || 3000
MongoClient.connect(
{
useNewUrlParser: true,
poolSize: 50,
wtimeout: 2500,
},
)
.catch(err => {
console.error(err.stack)
process.exit(1)
})
.then(async client => {
await ProductsDAO.injectDB(client)
await ServicesDAO.injectDB(client)
app.listen(port, () => {
console.log(`listening on port ${port}`)
})
})
但是现在我需要添加一些模式模型来支持插入和更新操作。所以我用的是猫鼬模式
let Products = mongoose.model('Products', productsSchema);
但为了有效地使用模式,mongoose希望建立如下连接:
//Setup database connection with mongoose
const mongoose = require('mongoose');
const url = 'mongodb://localhost:27017/mydb';
const connect = mongoose.connect(url);
connect.then((db) => {
console.log('Connected to database');
}, (err) => {console.log(err); });
如何避免通过mongoose建立连接,使用已经创建的与MongoClient的连接,并且仍然能够使用mongoose模式