SchemaObjectId


SchemaObjectId()

パラメータ
  • key «文字列»
  • オプション «オブジェクト»
継承

ObjectId スキーマタイプコンストラクタ。


SchemaObjectId.checkRequired()

パラメータ
  • fn «関数»
返り値
  • «関数»
  • «プロパティ»

requiredチェックに文字列を渡すかどうかを確認するために使用される、必須バリデータ関数をオーバーライドします。

// Allow empty strings to pass `required` check
mongoose.Schema.Types.String.checkRequired(v => v != null);

const M = mongoose.model({ str: { type: String, required: true } });
new M({ str: '' }).validateSync(); // `null`, validation passes!

SchemaObjectId.get()

パラメータ
  • getter «関数»
返り値
  • «this»
  • «プロパティ»

すべての ObjectId インスタンスにゲッターをアタッチします

// Always convert to string when getting an ObjectId
mongoose.ObjectId.get(v => v.toString());

const Model = mongoose.model('Test', new Schema({}));
typeof (new Model({})._id); // 'string'

SchemaObjectId.get()

パラメータ
  • caster «関数»
返り値
  • «関数»
  • «プロパティ»

任意の値をオブジェクト ID にキャストするために使用される関数を取得/設定します。

// Make Mongoose only try to cast length 24 strings. By default, any 12
// char string is a valid ObjectId.
const original = mongoose.ObjectId.cast();
mongoose.ObjectId.cast(v => {
  assert.ok(typeof v !== 'string' || v.length === 24);
  return original(v);
});

// Or disable casting entirely
mongoose.ObjectId.cast(false);

SchemaObjectId.prototype.auto()

パラメータ
  • turnOn «ブール値» 自動生成される ObjectId の既定値

返り値
  • «SchemaType» this

turnOn が true の場合に、自動生成された ObjectId 既定値を追加します。


SchemaObjectId.prototype.checkRequired()

パラメータ
  • value «任意の»
  • doc «Document»
返り値
  • «ブール値»

指定された値が必須のバリデータを満たしているかどうかを確認します。


SchemaObjectId.schemaName

  • «プロパティ»

このスキーマタイプの名前であり、関数名を変更する難読化ツールからの防御に使用されます。


SchemaObjectId.set()

パラメータ
  • option «文字列» 値を設定するオプション

  • value «任意の» オプションの値

返り値
  • «未定義、void»
  • «プロパティ»

すべての ObjectId インスタンスの既定のオプションを設定します。

// Make all object ids have option `required` equal to true.
mongoose.Schema.ObjectId.set('required', true);

const Order = mongoose.model('Order', new Schema({ userId: ObjectId }));
new Order({ }).validateSync().errors.userId.message; // Path `userId` is required.