如何更新猫鼬默认字符串架构属性修剪? [英] How to update mongoose default string schema property trim?
本文介绍了如何更新猫鼬默认字符串架构属性修剪?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望每个字符串属性的修剪默认设置为true. 有办法吗?
I would like to have every string property have trim set to true by default. Is there a way?
?? mongoose.Schema.String -> default { trim: true }
var schema = new Schema({
p1: { type: String },
p2: { type: String, trim: true }
p3: { type: String, trim: true }
p4: { type: String }
});
推荐答案
为架构路径重用通用配置的一种好方法是使用变量进行设置.
A good way to re-use common configurations for schema paths is to set them using a variable.
喜欢这个:
var trimmedString = { type: String, trim: true };
var schema = new Schema({
p1: trimmedString,
p2: trimmedString,
p3: trimmedString,
p4: trimmedString
});
您还可以从为您设置默认值的函数中返回定义,但是允许您进行两项覆盖(或添加其他设置,例如索引或默认值).
You could also return the definition from a function that sets defaults for you, but allows you two override things (or add other settings, like an index or default).
喜欢这个:
(使用下划线库的默认方法)
var _ = require('underscore');
var stringType = function(ops) {
return _.defaults(ops || {}, {
type: String,
trim: true
});
}
var schema = new Schema({
p1: stringType(),
p2: stringType({ index: true }),
p3: stringType({ default: "something" }),
p4: stringType({ trim: false })
});
这篇关于如何更新猫鼬默认字符串架构属性修剪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文