SchemaTypeOptions
SchemaTypeOptions()
SchemaTypeOptions.prototype.cast
SchemaTypeOptions.prototype.default
SchemaTypeOptions.prototype.immutable
SchemaTypeOptions.prototype.index
SchemaTypeOptions.prototype.ref
SchemaTypeOptions.prototype.ref
SchemaTypeOptions.prototype.required
SchemaTypeOptions.prototype.select
SchemaTypeOptions.prototype.sparse
SchemaTypeOptions.prototype.text
SchemaTypeOptions.prototype.transform
SchemaTypeOptions.prototype.type
SchemaTypeOptions.prototype.unique
SchemaTypeOptions.prototype.validate
SchemaTypeOptions()
Type
- «コンストラクタ»
スキーマタイプに定義されたオプション。
例
const schema = new Schema({ name: String });
schema.path('name').options instanceof mongoose.SchemaTypeOptions; // true
SchemaTypeOptions.prototype.cast
Type
- «String»
この個別のパスのキャストロジックをオーバーライドできます。文字列の場合、指定された文字列はMongooseのデフォルトのキャストエラーメッセージを上書きします。
例
const schema = new Schema({
num: {
type: Number,
cast: '{VALUE} is not a valid number'
}
});
// Throws 'CastError: "bad" is not a valid number'
schema.path('num').cast('bad');
const Model = mongoose.model('Test', schema);
const doc = new Model({ num: 'fail' });
const err = doc.validateSync();
err.errors['num']; // 'CastError: "fail" is not a valid number'
SchemaTypeOptions.prototype.default
Type
- «関数|任意»
このパスのデフォルト値。関数である場合、Mongooseはその関数を実行し、戻り値をデフォルトとして使用します。
SchemaTypeOptions.prototype.immutable
Type
- «関数|ブール値»
truthyの場合、ドキュメントが最初にデータベースに保存された後、Mongooseはこのパスの変更を許可しません。Mongooseの不変性について詳しくはこちらをご覧ください。
SchemaTypeOptions.prototype.index
Type
- «ブール値|数値|オブジェクト»
truthyの場合、モデルがコンパイルされるときに、Mongooseはこのパスにインデックスを作成します。
SchemaTypeOptions.prototype.ref
Type
- «関数|文字列»
このパスをポピュレートする場合にpopulate()
が使用するモデル。
SchemaTypeOptions.prototype.ref
Type
- «関数|文字列»
populate()
が使用するモデルを検索するために使用する必要があるドキュメント内のパス。
SchemaTypeOptions.prototype.required
Type
- «関数|ブール値»
trueの場合、このパスに必須バリデーターをアタッチします。これにより、このパスがnullish値に設定されないようにします。関数である場合、Mongooseはその関数を呼び出し、関数がtruthy値を返す場合にのみnullish値をチェックします。
SchemaTypeOptions.prototype.select
Type
- «ブール値|数値»
find()
、findOne()
などを使用してドキュメントをロードするときに、このパスをデフォルトで含めるか除外するか。
SchemaTypeOptions.prototype.sparse
Type
- «ブール値|数値»
truthyの場合、Mongooseはこのパスにスパースインデックスを作成します。
SchemaTypeOptions.prototype.text
Type
- «ブール値|数値|オブジェクト»
truthyの場合、Mongooseはこのパスにテキストインデックスを作成します。
SchemaTypeOptions.prototype.transform
Type
- «関数»
この個別のスキーマタイプの変換関数を定義します。toJSON()
またはtoObject()
を呼び出すときにのみ呼び出されます。
例
const schema = Schema({
myDate: {
type: Date,
transform: v => v.getFullYear()
}
});
const Model = mongoose.model('Test', schema);
const doc = new Model({ myDate: new Date('2019/06/01') });
doc.myDate instanceof Date; // true
const res = doc.toObject({ transform: true });
res.myDate; // 2019
SchemaTypeOptions.prototype.type
Type
- «関数|文字列|オブジェクト»
このパスをキャストする型。
SchemaTypeOptions.prototype.unique
Type
- «ブール値|数値»
truthyの場合、モデルがコンパイルされるときに、Mongooseはこのパスに一意のインデックスを作成します。unique
オプションはバリデーターではありません。
SchemaTypeOptions.prototype.validate
Type
- «関数|オブジェクト»
このスキーマタイプを検証する方法を記述する関数またはオブジェクト。