SchemaObjectId


SchemaObjectId()

參數
  • key «字串»
  • options «物件»
繼承自

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 實例附加一個 getter

範例

// 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 «函數»
回傳
  • «函數»
類型
  • «屬性»

取得/設定用於將任意值轉換為 ObjectId 的函數。

範例

// 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 «文件»
回傳
  • «布林值»

檢查給定的值是否滿足必要驗證器。


SchemaObjectId.schemaName

類型
  • «屬性»

這個結構描述類型的名稱,以防止縮小器混淆函數名稱。


SchemaObjectId.set()

參數
  • option «字串» 您想要設定值的選項

  • value «任意值» 選項的值

回傳
  • «undefined,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.