2013-11-04 79 views
14

猫鼬似乎默认为使所有字段不是必需的。有没有办法让所有在不改变各自所需的字段:使猫鼬需要的所有字段

Dimension = mongoose.Schema(
    name: String 
    value: String 
) 

Dimension = mongoose.Schema(
    name: 
    type: String 
    required: true 
    value: 
    type: String 
    required: true 
) 

它会得到十分可怕的,因为我有很多的这些。

+1

如果需要所有字段,你为什么要使用一个无模式的数据库? –

+3

无模式数据库与必填字段无关,您可以在关系数据库中使用非必填字段,反之亦然。 (在我看来) – fernandodof

回答

8

我落得这样做:

r_string = 
    type: String 
    required: true 

r_number = 
    type: Number 
    required: true 

以及其他数据类型。

+0

你最终放置在哪里? @ maxko87 – softcode

0

我不知道是否有这样做的猫鼬更简单的方法,但我会做以下在你的IDE /编辑:

列出你的领域,你通常会:

Dimension = mongoose.Schema(
    name: String 
    value: String 
) 

然后做一个查找和替换String{type: String, required: true},替换它给你:

Dimension = mongoose.Schema(
    name: {type: String, required: true}, 
    value: {type: String, required: true}, 
) 

那么对于Number等做T相同YPES。

3

那么你可以编写一个mongoose模式插件函数,它可以通过模式对象并调整它来使每个字段都是必需的。那么你只需要每个模式1行:Dimension.plugin(allRequired)

10

你可以这样做:

var schema = { 
    name: { type: String}, 
    value: { type: String} 
}; 

var requiredAttrs = ['name', 'value']; 

for (attr in requiredAttrs) { schema[attr].required = true; } 

var Dimension = mongoose.schema(schema); 

或所有ATTRS(使用下划线,这是真棒):

var schema = { 
    name: { type: String}, 
    value: { type: String} 
}; 

_.each(_.keys(schema), function (attr) { schema[attr].required = true; }); 

var Dimension = mongoose.schema(schema); 
+4

所需属性的隐式声明只是将问题放在一边,使其更加丑陋。 – Flint

2

猫鼬没有提供设置所有字段的方法,但你可以递归地做。

像Peter提到的那样,你可以插入它以重用代码。

递归设置:

// game.model.js 
var fields = require('./fields'); 
var Game = new Schema({ ... }); 

for(var p in Game.paths){ 
    Game.path(p).required(true); 
} 

Pluginized:

// fields.js 
module.exports = function (schema, options) { 
    if (options && options.required) { 
    for(var p in schema.paths){ 
     schema.path(p).required(true); 
    } 
    } 
} 

// game.model.js 
var fields = require('./fields'); 
var Game = new Schema({ ... }); 
Game.plugin(fields, { required: true }); 
4

所有字段的属性都在schema.paths[attribute]schema.path(attribute);

一个正确的路要走:在不需要一个字段定义,

Schema = mongoose.Schema; 
var Myschema = new Schema({ 
    name : { type:String }, 
    type : { type:String, required:false } 
}) 

,并让他们都默认需要:

function AllFieldsRequiredByDefautlt(schema) { 
    for (var i in schema.paths) { 
     var attribute = schema.paths[i] 
     if (attribute.isRequired == undefined) { 
      attribute.required(true); 
     } 
    } 
} 

AllFieldsRequiredByDefautlt(Myschema) 

下划线的方式:

_=require('underscore') 
_.each(_.keys(schema.paths), function (attr) { 
    if (schema.path(attr).isRequired == undefined) { 
     schema.path(attr).required(true); 
    } 
}) 

测试:

MyTable = mongoose.model('Myschema', Myschema); 
t = new MyTable() 
t.save() 
0

在以前的答案的基础上,下面的模块将使默认情况下所需的字段。以前的答案没有递归嵌套的对象/数组。

用法:

const rSchema = require("rschema"); 

var mySchema = new rSchema({ 
    request:{ 
     key:String, 
     value:String 
    }, 
    responses:[{ 
     key:String, 
     value:String 
    }] 
}); 

节点模块:

const Schema = require("mongoose").Schema; 

//Extends Mongoose Schema to require all fields by default 
module.exports = function(data){ 
    //Recursive 
    var makeRequired = function(schema){ 
     for (var i in schema.paths) { 
      var attribute = schema.paths[i]; 
      if (attribute.isRequired == undefined) { 
       attribute.required(true); 
      } 
      if (attribute.schema){ 
       makeRequired(attribute.schema); 
      } 
     } 
    }; 

    var schema = new Schema(data); 
    makeRequired(schema); 
    return schema; 
};