gpt4 book ai didi

node.js - 如何在 MongoDB/Mongoose 中动态添加架构

转载 作者:太空宇宙 更新时间:2023-11-03 22:49:39 25 4
gpt4 key购买 nike

我想创建一个由用户定义的数据库,每个用户都可以有自己风格的数据库。所以我使用了 strict: false 但现在的问题是我无法让用户定义模型下每个模式的 type

示例

const mongoose = require('mongoose');

const testSchema = new mongoose.Schema({
label: {
required: 'please enter label',
trim: true,
type: String
},
url: {
type: String,
trim: true,
},
settings: {} //User defined
}, {
timestamps: true, strict: false
});


module.exports = mongoose.model('test', testSchema);

在上述情况下,我希望该设置由用户定义就像,

{
"label": "About Us",
"url": "www.google.com",
"settings": {
"name": {
"type": "String", //Problem is Here, i can't send datatype directly
"required": true
},
"age": {
"type": "Number",
"required": true,
"enum": [10, 12]
}
}
}

所以请告诉我,我怎样才能让用户定义模式的类型?

最佳答案

strict: true 并不意味着您可以将任何内容传递到 settings 字段。

这意味着您的架构格式是动态的 - 您可以在文档中包含架构中未定义的意外字段名称。


回答您的问题:

似乎您想要子文档,让我们创建另一个架构并将其附加为类型:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const types = Schema.Types;

const testSettingsSchema = new Schema({
name: {
type: types.String,
required: true
},
age: {
type: types.Number,
required: true
enum: [10, 12]
}
},
{
_id : false,
timestamps: false,
strict: false
});

const testSchema = new Schema({
label: {
required: 'please enter label',
trim: true,
type: types.String
},
url: {
type: types.String,
trim: true,
},
settings: {
type: testSettingsSchema,
required: true
}
},
{
timestamps: true,
strict: true
});


module.exports = mongoose.model('test', testSchema);



但是为了获得更大的灵 active 并避免创建大的 test 文档(因为用户可能会推送不可预测的大对象),请创建另一个架构:指向 test_settings 的 testSettings 集合并引用 settings 字段:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const types = Schema.Types;

const testSettingsSchema = new Schema({
name: {
type: types.Mixed
},
age: {
type: types.Mixed
}
},
{
collection: 'test_settings',
timestamps: false,
strict: false // tells to mongoose that schema may "grow"
});
mongoose.model('testSettings', testSettingsSchema);

const testSchema = new Schema({
label: {
required: 'please enter label',
trim: true,
type: types.String
},
url: {
type: types.String,
trim: true,
},
settings: {
type: types.ObjectId,
ref: 'testSettings'
default: null
}
},
{
collection: 'tests',
timestamps: true,
strict: true
});


module.exports = mongoose.model('test', testSchema);

将其创建为:

const Test = mongoose.model('test');
const TestSettings = mongoose.model('testSettings');

app.post('/tests', async (req, res) => {
try {
const testSettings = await TestSettings.create(req.body.settings);

const test = new Test(req.body);
test.settings = testSettings._id;
await test.save();

res.status(201).send({_id: test._id});
}
catch(error) {
res.status(500).send({message: error.message});
}
});

并根据请求时间将其获取为:

const Test = mongoose.model('test');

app.get('/tests/:id', async (req, res) => {
try {
const test = await Test.findById(req.params.id)
.populate('settings')
.lean();
res.status(200).send(test);
}
catch(error) {
res.status(500).send({message: error.message});
}
});

关于node.js - 如何在 MongoDB/Mongoose 中动态添加架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52846415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com