javascript - 如何使用 mongoose 和 nodejs 在 mongodb 中以编程方式创建集合
问题描述
我正在尝试以编程方式在 MongoDB 中创建一个集合,但我不知道如何创建它。我有一个通用的模式模型,使用它如何创建具有不同名称的多个集合。如果有人知道请帮忙。
common.model.js:
const mongoose = require('mongoose');
var userSchemaOil = new mongoose.Schema({
pid: {
type: String
},
name: {
type: String
},
weight: {
type: String
},
price: {
type: Number
}
});
创建.controller.js:
module.exports.createCollection = (req, res, next) => {
let collectionName=req.query.collectionname; //Example: Tea
module.exports = mongoose.model( collectionName, userSchemaOil);
?
}
解决方案
推荐阅读
- field - 如何将我的工作区的所有字段移动到我的 dbtab 中,而不指定每个字段?
- c - 了解 ARM Cortex-M 中断向量表定义的 C 语法
- spring-boot - Spring Native 无法执行 org.graalvm.buildtools
- javascript - 根据可能的路径计算进度(JavaScript 问题)
- javascript - 无线电输入上的 onclick 事件未触发功能
- angular - 将整个应用程序标记为脏并强制更改检测
- calendar - 从一周的第 N 天开始打印月历
- angular - Leaflet - 如何从地图背景中移除旧的移动标记?
- c# - 如何使用 Net Framework 管理 C# 中的数千个级联事件?
- c# - 如何触发 DataReceivedEventHandler?