我想知道是否可以使用 Mongoose 模式、Node.js 和 Angular 在 mongodb 中动态创建表。
创建模式的基本方法是在 Node.js 中显式创建模型,如下所示:
import mongoose from 'mongoose';
const Schema = mongoose.Schema;
const postSchema = new Schema({
title: { type: 'String', required: true },
content: { type: 'String', required: true },
slug: { type: 'String', required: true }
});
let Post = mongoose.model('Post', postSchema);
是否可以使用 Angular 前端的用户输入动态创建此架构?
当然有可能...- 建议使用express https://npmjs.org/express作为服务器框架:
import mongoose from 'mongoose';
import { Router } from 'express';
const router = Router();
router.post('/newModel/', createNewModel);
function createNewModel(req, res, next) {
const Schema = mongoose.Schema;
// while req.body.model contains your model definition
mongoose.model(req.body.modelName, new Schema(req.body.model));
res.send('Created new model.');
}
……不过请小心!为用户提供一种如此轻松地修改数据库的方式通常不是一个好主意。
Update:格式与您想要的括号中的格式完全相同:
{
"title": { "type": "String", "required": "true" },
"content": { "type": "String", "required": "true" },
"slug": { "type": "String", "required": "true" }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)