首页 > 解决方案 > 如何使用 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);

   ?

    }

标签: javascriptnode.jsmongodbmongoose

解决方案


推荐阅读