Вы можете циклически переключаться между клавишами вашего Json, а затем добавлять объект с помощью [key]: { type: 'String', default: '' }
.Фрагмент кода запускает пример с простым объектом (не схемой).
let schema = {};
let jsonData = {
foo: 'foo45',
bar: 'bar',
toto: 'toto'
}
Object.keys(jsonData).map(function(key) {
schema[key] = { type: 'String', default: ''}
});
console.log(schema);
Адаптация для mongoose.Schema
const mySchema = new mongoose.Schema({});
const schemaData = require('./myData.json')
// map down to where you need to get your subpages
Object.keys(subpages).map(function(key) {
mySchema.add({
[key]: { type: 'String', default: ''}
});
});
console.log(mySchema.paths);
Это даст:
{ _id:
ObjectId {
path: '_id',
instance: 'ObjectID',
validators: [],
getters: [],
setters: [ [Function: resetId] ],
options: { auto: true, type: [Function] },
_index: null,
defaultValue: { [Function: defaultId] '$runBeforeSetters': true },
[Symbol(mongoose#schemaType)]: true },
foo:
SchemaString {
enumValues: [],
regExp: null,
path: 'foo',
instance: 'String',
validators: [],
getters: [],
setters: [],
options: { type: 'String', default: '' },
_index: null,
defaultValue: '',
[Symbol(mongoose#schemaType)]: true },
bar:
SchemaString {
enumValues: [],
regExp: null,
path: 'bar',
instance: 'String',
validators: [],
getters: [],
setters: [],
options: { type: 'String', default: '' },
_index: null,
defaultValue: '',
[Symbol(mongoose#schemaType)]: true },
toto:
SchemaString {
enumValues: [],
regExp: null,
path: 'toto',
instance: 'String',
validators: [],
getters: [],
setters: [],
options: { type: 'String', default: '' },
_index: null,
defaultValue: '',
[Symbol(mongoose#schemaType)]: true } }