问题描述
在我的模型定义中,我有:
In my model definition, I have:
appFeatures: [{
name: String,
param : [{
name : String,
value : String
}]
}]
我想将默认值设置为appFeatures,例如:
name:'feature',
param:[{name:'param1',value:' 1'},{name:'param2',value:'2'}]
I want to set default value to appFeatures, for example:name: 'feature',param: [{name:'param1',value:'1'},{name:'param2',value:'2'}]
我试图通过
appFeatures : { type : Array , "default" : ... }
但它没有用,有什么想法吗?
But its not working, any ideas?
谢谢
推荐答案
Mongoose允许您分离架构定义。两者都用于一般的重用和清晰的代码。所以更好的方法是:
Mongoose allows you to "separate" schema definitions. Both for general "re-use" and clarity of code. So a better way to do this is:
// general imports
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
// schema for params
var paramSchema = new Schema({
"name": { "type": String, "default": "something" },
"value": { "type": String, "default": "something" }
});
// schema for features
var featureSchema = new Schema({
"name": { "type": String, "default": "something" }
"params": [paramSchema]
});
var appSchema = new Schema({
"appFeatures": [featureSchema]
});
// Export something - or whatever you like
module.export.App = mongoose.model( "App", appSchema );
如果您愿意制作架构,那么它干净,可重复使用定义各个模块的一部分,并根据需要使用require系统导入。如果您不想模拟所有内容,您甚至可以从模型对象内省模式定义。
So it's "clean", and "re-usable" if you are willing to make "Schema" definitions part of individual modules and use the "require" system to import as needed. You can even "introspect" schema definitions from "model" objects if you don't want to "module" everything.
但大多数情况下,它允许您明确指定你想要什么默认值。
Mostly though, it allows you to clearly specify "what you want" for defaults.
对于更复杂的默认通道,你可能想要在预先保存钩子中这样做。作为一个更完整的例子:
For a more complex default through, you probably want to do this in a "pre save" hook instead. As a more complete example:
var async = require('async'),
mongoose = require('mongoose'),
Schema = mongoose.Schema;
var paramSchema = new Schema({
"name": { "type": String, "default": "something" },
"value": { "type": String, "default": "something" }
});
var featureSchema = new Schema({
"name": { "type": String, "default": "something" },
"params": [paramSchema]
});
var appSchema = new Schema({
"appFeatures": [featureSchema]
});
appSchema.pre("save",function(next) {
if ( !this.appFeatures || this.appFeatures.length == 0 ) {
this.appFeatures = [];
this.appFeatures.push({
"name": "something",
"params": []
})
}
this.appFeatures.forEach(function(feature) {
if ( !feature.params || feature.params.length == 0 ) {
feature.params = [];
feature.params.push(
{ "name": "a", "value": "A" },
{ "name": "b", "value": "B" }
);
}
});
next();
});
var App = mongoose.model( 'App', appSchema );
mongoose.connect('mongodb://localhost/test');
async.series(
[
function(callback) {
App.remove({},function(err,res) {
if (err) throw err;
callback(err,res);
});
},
function(callback) {
var app = new App();
app.save(function(err,doc) {
if (err) throw err;
console.log(
JSON.stringify( doc, undefined, 4 )
);
callback()
});
},
function(callback) {
App.find({},function(err,docs) {
if (err) throw err;
console.log(
JSON.stringify( docs, undefined, 4 )
);
callback();
});
}
],
function(err) {
if (err) throw err;
console.log("done");
mongoose.disconnect();
}
);
您可以清理它并内省架构路径以获取其他级别的默认值。但是你基本上想说如果没有定义内部数组,那么你将填写默认值为编码。
You could clean that up and introspect the schema path to get default values at other levels. But you basically want to say if that inner array is not defined then you are going to fill in the default values as coded.
这篇关于将默认值设置为节点js中的mongoose数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!