gpt4 book ai didi

mongoose - 如何更新 Mongoose 默认字符串架构属性修剪?

转载 作者:行者123 更新时间:2023-12-02 20:12:54 30 4
gpt4 key购买 nike

我希望每个字符串属性都默认设置为 true。有办法吗?

?? 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 }
});

最佳答案

重用架构路径的通用配置的一个好方法是使用变量来设置它们。

像这样:

var trimmedString = { type: String, trim: true };

var schema = new Schema({
p1: trimmedString,
p2: trimmedString,
p3: trimmedString,
p4: trimmedString
});

您还可以从为您设置默认值的函数返回定义,但允许您覆盖两个内容(或添加其他设置,例如索引或默认值)。

像这样:

(使用下划线库的 defaults method )

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 })
});

关于mongoose - 如何更新 Mongoose 默认字符串架构属性修剪?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14204611/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com