key «String» options «Object» ObjectId SchemaType constructor.
fn «Function» Override the function the required validator uses to check whether a string passes the required check.
// 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!
getter «Function» Attaches a getter for all ObjectId instances
// 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'
caster «Function» Get/set the function used to cast arbitrary values to objectids.
// 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);
turnOn «Boolean» auto generated ObjectId defaults Adds an auto-generated ObjectId default if turnOn is true.
value «Any» doc «Document» Check if the given value satisfies a required validator.
This schema type's name, to defend against minifiers that mangle function names.
option «String» The option you'd like to set the value for value «Any» value for option Sets a default option for all ObjectId instances.
// 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.
© 2010 LearnBoost
Licensed under the MIT License.
https://mongoosejs.com/docs/api/schemaobjectid.html