您好,在这篇文章link here之后,我尝试在app.js中切换集合
mongoose.connection.useDb(collection).model("Employee", employeeSchema);
可以请一个简短的例子来写例子吗?!。
我正在搜索它,但对我来说不起作用。
我还要附上我的架构外观的示例。 谢谢:)
app.js
mongoose.connect(process.env.MONGO_URI).then((result) => {
console.log("DB Connected");
})
let businessCollection;
app.use("/:businessUrl", (req, res, next) => {
console.log(req.params.businessUrl);
businessCollection = req.params.businessUrl;
next();
});
exports.businessCollection = businessCollection;
模式
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const { businessCollection } = require("../app");
const serviceSchema = new Schema(
{
title: {
type: String,
required: true,
},
}
);
module.exports = mongoose.connection.useDb(businessCollection)
.model("Employee", employeeSchema);