如何循环猫鼬模式中的项目
How to loop items in mongoose schema
我有 mongoose Schema ,其中有按语言分类的模式。我想循环它们取决于我的其他语言模型。现在我有它们 static(en, ru, ge).
const StrategyTranslatedFieldsSchema = mongoose.Schema(
{
title: String,
teaser: String
},
{ _id : false }
)
const StrategySchema = mongoose.Schema({
en: StrategyTranslatedFieldsSchema,
ge: StrategyTranslatedFieldsSchema,
ru: StrategyTranslatedFieldsSchema,
},
{
timestamps: true
});
我的语言模式:
const languageSchema = mongoose.Schema({
en:{
type: String,
default: 'en'
},
ru:{
type: String,
default: 'ru'
},
ge:{
type: String,
default: 'ge'
},
})
想要这样的东西:
const mongoose = require('mongoose');
const slug = require('mongoose-slug-updater');
const Language = require('../models/Language')
mongoose.plugin(slug);
const StrategyTranslatedFieldsSchema = mongoose.Schema(
{
title: String,
teaser: String
},
{ _id : false }
)
const StrategySchema = mongoose.Schema({
slug: {
type: String,
slug: "en.title",
slugPaddingSize: 2,
unique: true
},
status:{
type: Boolean,
default: true
},
for(let key in Language){
key: StrategyTranslatedFieldsSchema
}
},
{
timestamps: true
});
const Strategy = mongoose.model('strategy', StrategySchema);
module.exports = Strategy;
同样有趣的是,保存多语言数据是不是一个好习惯,比如那个例子?
谢谢
你可以这样做。
// create object you want to pass StrategySchema
const strategySchemaObject = {
slug: {
type: String,
slug: "en.title",
slugPaddingSize: 2,
unique: true
},
status:{
type: Boolean,
default: true
}
}
// add each field to your schema object
Object.keys(Language.schema.obj).forEach((lang) => {
strategySchemaObject[lang] = StrategyTranslatedFieldsSchema
})
// create your schema
const StrategySchema = mongoose.Schema(strategySchemaObject, {
timestamps: true
})
我有 mongoose Schema ,其中有按语言分类的模式。我想循环它们取决于我的其他语言模型。现在我有它们 static(en, ru, ge).
const StrategyTranslatedFieldsSchema = mongoose.Schema(
{
title: String,
teaser: String
},
{ _id : false }
)
const StrategySchema = mongoose.Schema({
en: StrategyTranslatedFieldsSchema,
ge: StrategyTranslatedFieldsSchema,
ru: StrategyTranslatedFieldsSchema,
},
{
timestamps: true
});
我的语言模式:
const languageSchema = mongoose.Schema({
en:{
type: String,
default: 'en'
},
ru:{
type: String,
default: 'ru'
},
ge:{
type: String,
default: 'ge'
},
})
想要这样的东西:
const mongoose = require('mongoose');
const slug = require('mongoose-slug-updater');
const Language = require('../models/Language')
mongoose.plugin(slug);
const StrategyTranslatedFieldsSchema = mongoose.Schema(
{
title: String,
teaser: String
},
{ _id : false }
)
const StrategySchema = mongoose.Schema({
slug: {
type: String,
slug: "en.title",
slugPaddingSize: 2,
unique: true
},
status:{
type: Boolean,
default: true
},
for(let key in Language){
key: StrategyTranslatedFieldsSchema
}
},
{
timestamps: true
});
const Strategy = mongoose.model('strategy', StrategySchema);
module.exports = Strategy;
同样有趣的是,保存多语言数据是不是一个好习惯,比如那个例子? 谢谢
你可以这样做。
// create object you want to pass StrategySchema
const strategySchemaObject = {
slug: {
type: String,
slug: "en.title",
slugPaddingSize: 2,
unique: true
},
status:{
type: Boolean,
default: true
}
}
// add each field to your schema object
Object.keys(Language.schema.obj).forEach((lang) => {
strategySchemaObject[lang] = StrategyTranslatedFieldsSchema
})
// create your schema
const StrategySchema = mongoose.Schema(strategySchemaObject, {
timestamps: true
})