SchemaObjectId
SchemaObjectId()SchemaObjectId.checkRequired()SchemaObjectId.get()SchemaObjectId.get()SchemaObjectId.prototype.auto()SchemaObjectId.prototype.checkRequired()SchemaObjectId.schemaNameSchemaObjectId.set()
SchemaObjectId()
参数
key«String»options«Object»
继承
ObjectId SchemaType 构造函数。
SchemaObjectId.checkRequired()
参数
fn«Function»
返回
- «Function»
类型
- «property»
覆盖用于检查字符串是否通过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«Function»
返回
- «this»
类型
- «property»
为所有 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«Function»
返回
- «Function»
类型
- «property»
获取/设置用于将任意值强制转换为 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«Boolean» 自动生成的 ObjectId 默认值
返回
- «SchemaType» this
如果 turnOn 为 true,则添加自动生成的 ObjectId 默认值。
SchemaObjectId.prototype.checkRequired()
参数
value«Any»doc«Document»
返回
- «Boolean»
检查给定值是否满足必需验证器。
SchemaObjectId.schemaName
类型
- «property»
此模式类型的名称,用于防御混淆函数名称的缩小器。
SchemaObjectId.set()
参数
option«String» 您想设置值的选项value«Any» 选项的值
返回
- «undefined,void»
类型
- «property»
为所有 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. 
