Advanced Schemas
Redirecting
Aggregate
AggregationCursor
Array
ArraySubdocument
ArraySubdocument.prototype.$parent()
Returns this sub-documents parent document.
ArraySubdocument.prototype.parentArray()
Returns this subdocument's parent array.
Example:
-const Test = mongoose.model('Test', new Schema({
- docArr: [{ name: String }]
+const Test = mongoose.model('Test', new Schema({
+ docArr: [{ name: String }]
}));
-const doc = new Test({ docArr: [{ name: 'test subdoc' }] });
+const doc = new Test({ docArr: [{ name: 'test subdoc' }] });
-doc.docArr[0].parentArray() === doc.docArr; // true
+doc.docArr[0].parentArray() === doc.docArr; // true
Decimal128
Decimal128()
Decimal128 type constructor
Example:
-const id = new mongoose.Types.Decimal128('3.1415');
+const id = new mongoose.Types.Decimal128('3.1415');
DocumentArray
Error
QueryCursor
SchemaArrayOptions
SchemaArrayOptions()
SchemaArrayOptions.prototype.castNonArrays
SchemaArrayOptions.prototype.enum
SchemaArrayOptions.prototype.of
SchemaArrayOptions()
Type:
- «constructor»
Inherits:
The options defined on an Array schematype.
Example:
-const schema = new Schema({ tags: [String] });
-schema.path('tags').options; // SchemaArrayOptions instance
+const schema = new Schema({ tags: [String] });
+schema.path('tags').options; // SchemaArrayOptions instance
SchemaArrayOptions.prototype.castNonArrays
Type:
- «Boolean»
If set to false
, will always deactivate casting non-array values to arrays.
If set to true
, will cast non-array values to arrays if init
and SchemaArray.options.castNonArrays
are also true
Example:
-const Model = db.model('Test', new Schema({ x1: { castNonArrays: false, type: [String] } }));
-const doc = new Model({ x1: "some non-array value" });
-await doc.validate(); // Errors with "CastError"
+const Model = db.model('Test', new Schema({ x1: { castNonArrays: false, type: [String] } }));
+const doc = new Model({ x1: "some non-array value" });
+await doc.validate(); // Errors with "CastError"
SchemaArrayOptions.prototype.enum
Type:
- «Array»
If this is an array of strings, an array of allowed values for this path. Throws an error if this array isn't an array of strings.
SchemaArrayOptions.prototype.of
Type:
- «Function|String»
If set, specifies the type of this array's values. Equivalent to setting
type
to an array whose first element is of
.
Example:
-// `arr` is an array of numbers.
-new Schema({ arr: [Number] });
-// Equivalent way to define `arr` as an array of numbers
-new Schema({ arr: { type: Array, of: Number } });
+// `arr` is an array of numbers.
+new Schema({ arr: [Number] });
+// Equivalent way to define `arr` as an array of numbers
+new Schema({ arr: { type: Array, of: Number } });
SchemaBufferOptions
SchemaBufferOptions()
Type:
- «constructor»
Inherits:
The options defined on a Buffer schematype.
Example:
-const schema = new Schema({ bitmap: Buffer });
-schema.path('bitmap').options; // SchemaBufferOptions instance
+const schema = new Schema({ bitmap: Buffer });
+schema.path('bitmap').options; // SchemaBufferOptions instance
SchemaBufferOptions.prototype.subtype
Type:
- «Number»
Set the default subtype for this buffer.
SchemaDateOptions
SchemaDateOptions()
SchemaDateOptions.prototype.expires
SchemaDateOptions.prototype.max
SchemaDateOptions.prototype.min
SchemaDateOptions()
Type:
- «constructor»
Inherits:
The options defined on a Date schematype.
Example:
-const schema = new Schema({ startedAt: Date });
-schema.path('startedAt').options; // SchemaDateOptions instance
+const schema = new Schema({ startedAt: Date });
+schema.path('startedAt').options; // SchemaDateOptions instance
SchemaDateOptions.prototype.expires
Type:
- «Date»
If set, Mongoose creates a TTL index on this path.
mongo TTL index expireAfterSeconds
value will take 'expires' value expressed in seconds.
Example:
-const schema = new Schema({ "expireAt": { type: Date, expires: 11 } });
-// if 'expireAt' is set, then document expires at expireAt + 11 seconds
+const schema = new Schema({ "expireAt": { type: Date, expires: 11 } });
+// if 'expireAt' is set, then document expires at expireAt + 11 seconds
SchemaDateOptions.prototype.max
Type:
- «Date»
If set, Mongoose adds a validator that checks that this path is before the
given max
.
SchemaNumberOptions
SchemaNumberOptions()
SchemaNumberOptions.prototype.enum
SchemaNumberOptions.prototype.max
SchemaNumberOptions.prototype.min
SchemaNumberOptions.prototype.populate
SchemaNumberOptions()
Type:
- «constructor»
Inherits:
The options defined on a Number schematype.
Example:
-const schema = new Schema({ count: Number });
-schema.path('count').options; // SchemaNumberOptions instance
+const schema = new Schema({ count: Number });
+schema.path('count').options; // SchemaNumberOptions instance
SchemaNumberOptions.prototype.enum
Type:
- «Array»
If set, Mongoose adds a validator that checks that this path is strictly equal to one of the given values.
Example:
-const schema = new Schema({
- favoritePrime: {
- type: Number,
- enum: [3, 5, 7]
+const schema = new Schema({
+ favoritePrime: {
+ type: Number,
+ enum: [3, 5, 7]
}
});
-schema.path('favoritePrime').options.enum; // [3, 5, 7]
+schema.path('favoritePrime').options.enum; // [3, 5, 7]
SchemaNumberOptions.prototype.max
Type:
- «Number»
If set, Mongoose adds a validator that checks that this path is less than the
given max
.
Example:
givenmin
.
SchemaNumberOptions.prototype.populate
Type:
- «Object»
Sets default populate options.
Example:
-const schema = new Schema({
- child: {
- type: Number,
- ref: 'Child',
- populate: { select: 'name' }
+const schema = new Schema({
+ child: {
+ type: Number,
+ ref: 'Child',
+ populate: { select: 'name' }
}
});
-const Parent = mongoose.model('Parent', schema);
+const Parent = mongoose.model('Parent', schema);
-// Automatically adds `.select('name')`
-Parent.findOne().populate('child');
+// Automatically adds `.select('name')`
+Parent.findOne().populate('child');
SchemaObjectIdOptions
SchemaObjectIdOptions()
Type:
- «constructor»
Inherits:
The options defined on an ObjectId schematype.
Example:
-const schema = new Schema({ testId: mongoose.ObjectId });
-schema.path('testId').options; // SchemaObjectIdOptions instance
+const schema = new Schema({ testId: mongoose.ObjectId });
+schema.path('testId').options; // SchemaObjectIdOptions instance
SchemaObjectIdOptions.prototype.auto
Type:
- «Boolean»
If truthy, uses Mongoose's default built-in ObjectId path.
SchemaObjectIdOptions.prototype.populate
Type:
- «Object»
Sets default populate options.
Example:
-const schema = new Schema({
- child: {
- type: 'ObjectId',
- ref: 'Child',
- populate: { select: 'name' }
+const schema = new Schema({
+ child: {
+ type: 'ObjectId',
+ ref: 'Child',
+ populate: { select: 'name' }
}
});
-const Parent = mongoose.model('Parent', schema);
+const Parent = mongoose.model('Parent', schema);
-// Automatically adds `.select('name')`
-Parent.findOne().populate('child');
+// Automatically adds `.select('name')`
+Parent.findOne().populate('child');
SchemaStringOptions
SchemaStringOptions()
SchemaStringOptions.prototype.enum
SchemaStringOptions.prototype.lowercase
SchemaStringOptions.prototype.match
SchemaStringOptions.prototype.maxLength
SchemaStringOptions.prototype.minLength
SchemaStringOptions.prototype.populate
SchemaStringOptions.prototype.trim
SchemaStringOptions.prototype.uppercase
SchemaStringOptions()
Type:
- «constructor»
Inherits:
The options defined on a string schematype.
Example:
-const schema = new Schema({ name: String });
-schema.path('name').options; // SchemaStringOptions instance
+const schema = new Schema({ name: String });
+schema.path('name').options; // SchemaStringOptions instance
SchemaStringOptions.prototype.enum
Type:
- «Array»
Array of allowed values for this path
SchemaStringOptions.prototype.lowercase
Type:
- «Boolean»
If truthy, Mongoose will add a custom setter that lowercases this string diff --git a/docs/api/schemasubdocument.html b/docs/api/schemasubdocument.html index 4896acd9a9b..593ef2eca2f 100644 --- a/docs/api/schemasubdocument.html +++ b/docs/api/schemasubdocument.html @@ -1,4 +1,4 @@ -
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:
- «constructor»
The options defined on a schematype.
Example:
-const schema = new Schema({ name: String });
-schema.path('name').options instanceof mongoose.SchemaTypeOptions; // true
+const schema = new Schema({ name: String });
+schema.path('name').options instanceof mongoose.SchemaTypeOptions; // true
SchemaTypeOptions.prototype.cast
Type:
- «String»
Allows overriding casting logic for this individual path. If a string, the given string overwrites Mongoose's default cast error message.
Example:
-const schema = new Schema({
- num: {
- type: Number,
- cast: '{VALUE} is not a valid number'
+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');
+// 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();
+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'
+err.errors['num']; // 'CastError: "fail" is not a valid number'
SchemaTypeOptions.prototype.default
Type:
- «Function|Any»
The default value for this path. If a function, Mongoose executes the function and uses the return value as the default.
@@ -60,19 +60,19 @@Example:
SchemaTypeOptions.prototype.transform
Type:
- «Function»
Define a transform function for this individual schema type.
Only called when calling toJSON()
or toObject()
.
Example:
-const schema = Schema({
- myDate: {
- type: Date,
- transform: v => v.getFullYear()
+const schema = Schema({
+ myDate: {
+ type: Date,
+ transform: v => v.getFullYear()
}
});
-const Model = mongoose.model('Test', schema);
+const Model = mongoose.model('Test', schema);
-const doc = new Model({ myDate: new Date('2019/06/01') });
-doc.myDate instanceof Date; // true
+const doc = new Model({ myDate: new Date('2019/06/01') });
+doc.myDate instanceof Date; // true
-const res = doc.toObject({ transform: true });
-res.myDate; // 2019
+const res = doc.toObject({ transform: true });
+res.myDate; // 2019
SchemaTypeOptions.prototype.type
Type:
- «Function|String|Object»
The type to cast this path to.
SchemaTypeOptions.prototype.unique
Type:
- «Boolean|Number»
If truthy, Mongoose diff --git a/docs/api/subdocument.html b/docs/api/subdocument.html index ece9cb8935d..d8e162b9c8b 100644 --- a/docs/api/subdocument.html +++ b/docs/api/subdocument.html @@ -1,4 +1,4 @@ -