mongoose 6.4.6 → 6.4.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/aggregate.js +32 -30
- package/lib/browser.js +1 -1
- package/lib/cast.js +2 -2
- package/lib/collection.js +1 -1
- package/lib/connection.js +34 -16
- package/lib/document.js +174 -101
- package/lib/index.js +2 -2
- package/lib/model.js +10 -10
- package/lib/query.js +10 -10
- package/lib/schema.js +1 -1
- package/lib/schematype.js +95 -81
- package/lib/virtualtype.js +14 -16
- package/package.json +2 -2
- package/types/connection.d.ts +1 -1
- package/types/models.d.ts +5 -5
package/lib/index.js
CHANGED
|
@@ -191,7 +191,7 @@ Mongoose.prototype.setDriver = function setDriver(driver) {
|
|
|
191
191
|
* Currently supported options are:
|
|
192
192
|
* - 'applyPluginsToChildSchemas': `true` by default. Set to false to skip applying global plugins to child schemas
|
|
193
193
|
* - 'applyPluginsToDiscriminators': `false` by default. Set to true to apply global plugins to discriminator schemas. This typically isn't necessary because plugins are applied to the base schema and discriminators copy all middleware, methods, statics, and properties from the base schema.
|
|
194
|
-
* - 'autoCreate': Set to `true` to make Mongoose call [`Model.createCollection()`](/docs/api/model.html#model_Model
|
|
194
|
+
* - 'autoCreate': Set to `true` to make Mongoose call [`Model.createCollection()`](/docs/api/model.html#model_Model-createCollection) automatically when you create a model with `mongoose.model()` or `conn.model()`. This is useful for testing transactions, change streams, and other features that require the collection to exist.
|
|
195
195
|
* - 'autoIndex': `true` by default. Set to false to disable automatic index creation for all models associated with this Mongoose instance.
|
|
196
196
|
* - 'debug': If `true`, prints the operations mongoose sends to MongoDB to the console. If a writable stream is passed, it will log to that stream, without colorization. If a callback function is passed, it will receive the collection name, the method name, then all arguments passed to the method. For example, if you wanted to replicate the default logging, you could output from the callback `Mongoose: ${collectionName}.${methodName}(${methodArgs.join(', ')})`.
|
|
197
197
|
* - 'returnOriginal': If `false`, changes the default `returnOriginal` option to `findOneAndUpdate()`, `findByIdAndUpdate`, and `findOneAndReplace()` to false. This is equivalent to setting the `new` option to `true` for `findOneAndX()` calls by default. Read our [`findOneAndUpdate()` tutorial](/docs/tutorials/findoneandupdate.html) for more information.
|
|
@@ -861,7 +861,7 @@ Mongoose.prototype.SchemaType = SchemaType;
|
|
|
861
861
|
* _Alias of mongoose.Schema.Types for backwards compatibility._
|
|
862
862
|
*
|
|
863
863
|
* @property SchemaTypes
|
|
864
|
-
* @see Schema.SchemaTypes #schema_Schema
|
|
864
|
+
* @see Schema.SchemaTypes #schema_Schema-Types
|
|
865
865
|
* @api public
|
|
866
866
|
*/
|
|
867
867
|
|
package/lib/model.js
CHANGED
|
@@ -2302,7 +2302,7 @@ Model.estimatedDocumentCount = function estimatedDocumentCount(options, callback
|
|
|
2302
2302
|
* });
|
|
2303
2303
|
*
|
|
2304
2304
|
* If you want to count all documents in a large collection,
|
|
2305
|
-
* use the [`estimatedDocumentCount()` function](/docs/api.html#model_Model
|
|
2305
|
+
* use the [`estimatedDocumentCount()` function](/docs/api.html#model_Model-estimatedDocumentCount)
|
|
2306
2306
|
* instead. If you call `countDocuments({})`, MongoDB will always execute
|
|
2307
2307
|
* a full collection scan and **not** use any indexes.
|
|
2308
2308
|
*
|
|
@@ -2347,8 +2347,8 @@ Model.countDocuments = function countDocuments(conditions, options, callback) {
|
|
|
2347
2347
|
* Counts number of documents that match `filter` in a database collection.
|
|
2348
2348
|
*
|
|
2349
2349
|
* This method is deprecated. If you want to count the number of documents in
|
|
2350
|
-
* a collection, e.g. `count({})`, use the [`estimatedDocumentCount()` function](/docs/api.html#model_Model
|
|
2351
|
-
* instead. Otherwise, use the [`countDocuments()`](/docs/api.html#model_Model
|
|
2350
|
+
* a collection, e.g. `count({})`, use the [`estimatedDocumentCount()` function](/docs/api.html#model_Model-estimatedDocumentCount)
|
|
2351
|
+
* instead. Otherwise, use the [`countDocuments()`](/docs/api.html#model_Model-countDocuments) function instead.
|
|
2352
2352
|
*
|
|
2353
2353
|
* #### Example:
|
|
2354
2354
|
*
|
|
@@ -2516,7 +2516,7 @@ Model.$where = function $where() {
|
|
|
2516
2516
|
* @param {ClientSession} [options.session=null] The session associated with this query. See [transactions docs](/docs/transactions.html).
|
|
2517
2517
|
* @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)
|
|
2518
2518
|
* @param {Boolean} [options.timestamps=null] If set to `false` and [schema-level timestamps](/docs/guide.html#timestamps) are enabled, skip timestamps for this update. Note that this allows you to overwrite timestamps. Does nothing if schema-level timestamps are not set.
|
|
2519
|
-
* @param {Boolean} [options.overwrite=false] By default, if you don't include any [update operators](https://docs.mongodb.com/manual/reference/operator/update/) in `update`, Mongoose will wrap `update` in `$set` for you. This prevents you from accidentally overwriting the document. This option tells Mongoose to skip adding `$set`. An alternative to this would be using [Model.findOneAndReplace(conditions, update, options, callback)](https://mongoosejs.com/docs/api/model.html#model_Model
|
|
2519
|
+
* @param {Boolean} [options.overwrite=false] By default, if you don't include any [update operators](https://docs.mongodb.com/manual/reference/operator/update/) in `update`, Mongoose will wrap `update` in `$set` for you. This prevents you from accidentally overwriting the document. This option tells Mongoose to skip adding `$set`. An alternative to this would be using [Model.findOneAndReplace(conditions, update, options, callback)](https://mongoosejs.com/docs/api/model.html#model_Model-findOneAndReplace).
|
|
2520
2520
|
* @param {Boolean} [options.upsert=false] if true, and no documents found, insert a new document
|
|
2521
2521
|
* @param {Object|String|String[]} [options.projection=null] optional fields to return, see [`Query.prototype.select()`](#query_Query-select)
|
|
2522
2522
|
* @param {Boolean} [options.new=false] if true, return the modified document rather than the original
|
|
@@ -2647,7 +2647,7 @@ function _decorateUpdateWithVersionKey(update, options, versionKey) {
|
|
|
2647
2647
|
* @param {ClientSession} [options.session=null] The session associated with this query. See [transactions docs](/docs/transactions.html).
|
|
2648
2648
|
* @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)
|
|
2649
2649
|
* @param {Boolean} [options.timestamps=null] If set to `false` and [schema-level timestamps](/docs/guide.html#timestamps) are enabled, skip timestamps for this update. Note that this allows you to overwrite timestamps. Does nothing if schema-level timestamps are not set.
|
|
2650
|
-
* @param {Boolean} [options.overwrite=false] By default, if you don't include any [update operators](https://docs.mongodb.com/manual/reference/operator/update/) in `update`, Mongoose will wrap `update` in `$set` for you. This prevents you from accidentally overwriting the document. This option tells Mongoose to skip adding `$set`. An alternative to this would be using [Model.findOneAndReplace({ _id: id }, update, options, callback)](https://mongoosejs.com/docs/api/model.html#model_Model
|
|
2650
|
+
* @param {Boolean} [options.overwrite=false] By default, if you don't include any [update operators](https://docs.mongodb.com/manual/reference/operator/update/) in `update`, Mongoose will wrap `update` in `$set` for you. This prevents you from accidentally overwriting the document. This option tells Mongoose to skip adding `$set`. An alternative to this would be using [Model.findOneAndReplace({ _id: id }, update, options, callback)](https://mongoosejs.com/docs/api/model.html#model_Model-findOneAndReplace).
|
|
2651
2651
|
* @param {Object|String} [options.sort] if multiple docs are found by the conditions, sets the sort order to choose which doc to update.
|
|
2652
2652
|
* @param {Boolean} [options.runValidators] if true, runs [update validators](/docs/validation.html#update-validators) on this command. Update validators validate the update operation against the model's schema
|
|
2653
2653
|
* @param {Boolean} [options.setDefaultsOnInsert=true] If `setDefaultsOnInsert` and `upsert` are true, mongoose will apply the [defaults](https://mongoosejs.com/docs/defaults.html) specified in the model's schema if a new document is created
|
|
@@ -2657,7 +2657,7 @@ function _decorateUpdateWithVersionKey(update, options, versionKey) {
|
|
|
2657
2657
|
* @param {Object|String} [options.select] sets the document fields to return.
|
|
2658
2658
|
* @param {Function} [callback]
|
|
2659
2659
|
* @return {Query}
|
|
2660
|
-
* @see Model.findOneAndUpdate #model_Model
|
|
2660
|
+
* @see Model.findOneAndUpdate #model_Model-findOneAndUpdate
|
|
2661
2661
|
* @see mongodb https://www.mongodb.org/display/DOCS/findAndModify+Command
|
|
2662
2662
|
* @api public
|
|
2663
2663
|
*/
|
|
@@ -2778,7 +2778,7 @@ Model.findOneAndDelete = function(conditions, options, callback) {
|
|
|
2778
2778
|
* @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)
|
|
2779
2779
|
* @param {Function} [callback]
|
|
2780
2780
|
* @return {Query}
|
|
2781
|
-
* @see Model.findOneAndRemove #model_Model
|
|
2781
|
+
* @see Model.findOneAndRemove #model_Model-findOneAndRemove
|
|
2782
2782
|
* @see mongodb https://www.mongodb.org/display/DOCS/findAndModify+Command
|
|
2783
2783
|
*/
|
|
2784
2784
|
|
|
@@ -2974,7 +2974,7 @@ Model.findOneAndRemove = function(conditions, options, callback) {
|
|
|
2974
2974
|
* @param {Object|String} [options.select] sets the document fields to return.
|
|
2975
2975
|
* @param {Function} [callback]
|
|
2976
2976
|
* @return {Query}
|
|
2977
|
-
* @see Model.findOneAndRemove #model_Model
|
|
2977
|
+
* @see Model.findOneAndRemove #model_Model-findOneAndRemove
|
|
2978
2978
|
* @see mongodb https://www.mongodb.org/display/DOCS/findAndModify+Command
|
|
2979
2979
|
*/
|
|
2980
2980
|
|
|
@@ -3054,7 +3054,7 @@ Model.create = function create(doc, options, callback) {
|
|
|
3054
3054
|
// to use a spread to specify options, see gh-7535
|
|
3055
3055
|
utils.warn('WARNING: to pass a `session` to `Model.create()` in ' +
|
|
3056
3056
|
'Mongoose, you **must** pass an array as the first argument. See: ' +
|
|
3057
|
-
'https://mongoosejs.com/docs/api.html#model_Model
|
|
3057
|
+
'https://mongoosejs.com/docs/api.html#model_Model-create');
|
|
3058
3058
|
}
|
|
3059
3059
|
}
|
|
3060
3060
|
|
|
@@ -3463,7 +3463,7 @@ function _setIsNew(doc, val) {
|
|
|
3463
3463
|
*
|
|
3464
3464
|
* This function does **not** trigger any middleware, neither `save()`, nor `update()`.
|
|
3465
3465
|
* If you need to trigger
|
|
3466
|
-
* `save()` middleware for every document use [`create()`](https://mongoosejs.com/docs/api.html#model_Model
|
|
3466
|
+
* `save()` middleware for every document use [`create()`](https://mongoosejs.com/docs/api.html#model_Model-create) instead.
|
|
3467
3467
|
*
|
|
3468
3468
|
* #### Example:
|
|
3469
3469
|
*
|
package/lib/query.js
CHANGED
|
@@ -2133,7 +2133,7 @@ Query.prototype._unsetCastError = function _unsetCastError() {
|
|
|
2133
2133
|
* Below are the current Mongoose-specific options.
|
|
2134
2134
|
*
|
|
2135
2135
|
* - `populate`: an array representing what paths will be populated. Should have one entry for each call to [`Query.prototype.populate()`](/docs/api.html#query_Query-populate)
|
|
2136
|
-
* - `lean`: if truthy, Mongoose will not [hydrate](/docs/api.html#model_Model
|
|
2136
|
+
* - `lean`: if truthy, Mongoose will not [hydrate](/docs/api.html#model_Model-hydrate) any documents that are returned from this query. See [`Query.prototype.lean()`](/docs/api.html#query_Query-lean) for more information.
|
|
2137
2137
|
* - `strict`: controls how Mongoose handles keys that aren't in the schema for updates. This option is `true` by default, which means Mongoose will silently strip any paths in the update that aren't in the schema. See the [`strict` mode docs](/docs/guide.html#strict) for more information.
|
|
2138
2138
|
* - `strictQuery`: controls how Mongoose handles keys that aren't in the schema for the query `filter`. This option is `false` by default for backwards compatibility, which means Mongoose will allow `Model.find({ foo: 'bar' })` even if `foo` is not in the schema. See the [`strictQuery` docs](/docs/guide.html#strictQuery) for more information.
|
|
2139
2139
|
* - `nearSphere`: use `$nearSphere` instead of `near()`. See the [`Query.prototype.nearSphere()` docs](/docs/api.html#query_Query-nearSphere)
|
|
@@ -4245,7 +4245,7 @@ Query.prototype.validate = function validate(castedDoc, options, isOverwriting,
|
|
|
4245
4245
|
* Internal thunk for .update()
|
|
4246
4246
|
*
|
|
4247
4247
|
* @param {Function} callback
|
|
4248
|
-
* @see Model.update #model_Model
|
|
4248
|
+
* @see Model.update #model_Model-update
|
|
4249
4249
|
* @api private
|
|
4250
4250
|
*/
|
|
4251
4251
|
Query.prototype._execUpdate = wrapThunk(function(callback) {
|
|
@@ -4256,7 +4256,7 @@ Query.prototype._execUpdate = wrapThunk(function(callback) {
|
|
|
4256
4256
|
* Internal thunk for .updateMany()
|
|
4257
4257
|
*
|
|
4258
4258
|
* @param {Function} callback
|
|
4259
|
-
* @see Model.update #model_Model
|
|
4259
|
+
* @see Model.update #model_Model-update
|
|
4260
4260
|
* @api private
|
|
4261
4261
|
*/
|
|
4262
4262
|
Query.prototype._updateMany = wrapThunk(function(callback) {
|
|
@@ -4267,7 +4267,7 @@ Query.prototype._updateMany = wrapThunk(function(callback) {
|
|
|
4267
4267
|
* Internal thunk for .updateOne()
|
|
4268
4268
|
*
|
|
4269
4269
|
* @param {Function} callback
|
|
4270
|
-
* @see Model.update #model_Model
|
|
4270
|
+
* @see Model.update #model_Model-update
|
|
4271
4271
|
* @api private
|
|
4272
4272
|
*/
|
|
4273
4273
|
Query.prototype._updateOne = wrapThunk(function(callback) {
|
|
@@ -4278,7 +4278,7 @@ Query.prototype._updateOne = wrapThunk(function(callback) {
|
|
|
4278
4278
|
* Internal thunk for .replaceOne()
|
|
4279
4279
|
*
|
|
4280
4280
|
* @param {Function} callback
|
|
4281
|
-
* @see Model.replaceOne #model_Model
|
|
4281
|
+
* @see Model.replaceOne #model_Model-replaceOne
|
|
4282
4282
|
* @api private
|
|
4283
4283
|
*/
|
|
4284
4284
|
Query.prototype._replaceOne = wrapThunk(function(callback) {
|
|
@@ -4368,7 +4368,7 @@ Query.prototype._replaceOne = wrapThunk(function(callback) {
|
|
|
4368
4368
|
* @param {Boolean} [options.timestamps=null] If set to `false` and [schema-level timestamps](/docs/guide.html#timestamps) are enabled, skip timestamps for this update. Does nothing if schema-level timestamps are not set.
|
|
4369
4369
|
* @param {Function} [callback] params are (error, writeOpResult)
|
|
4370
4370
|
* @return {Query} this
|
|
4371
|
-
* @see Model.update #model_Model
|
|
4371
|
+
* @see Model.update #model_Model-update
|
|
4372
4372
|
* @see Query docs https://mongoosejs.com/docs/queries.html
|
|
4373
4373
|
* @see update https://docs.mongodb.org/manual/reference/method/db.collection.update/
|
|
4374
4374
|
* @see writeOpResult https://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#~WriteOpResult
|
|
@@ -4432,7 +4432,7 @@ Query.prototype.update = function(conditions, doc, options, callback) {
|
|
|
4432
4432
|
* @param {Boolean} [options.timestamps=null] If set to `false` and [schema-level timestamps](/docs/guide.html#timestamps) are enabled, skip timestamps for this update. Does nothing if schema-level timestamps are not set.
|
|
4433
4433
|
* @param {Function} [callback] params are (error, writeOpResult)
|
|
4434
4434
|
* @return {Query} this
|
|
4435
|
-
* @see Model.update #model_Model
|
|
4435
|
+
* @see Model.update #model_Model-update
|
|
4436
4436
|
* @see Query docs https://mongoosejs.com/docs/queries.html
|
|
4437
4437
|
* @see update https://docs.mongodb.org/manual/reference/method/db.collection.update/
|
|
4438
4438
|
* @see writeOpResult https://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#~WriteOpResult
|
|
@@ -4497,7 +4497,7 @@ Query.prototype.updateMany = function(conditions, doc, options, callback) {
|
|
|
4497
4497
|
* @param {Boolean} [options.timestamps=null] If set to `false` and [schema-level timestamps](/docs/guide.html#timestamps) are enabled, skip timestamps for this update. Note that this allows you to overwrite timestamps. Does nothing if schema-level timestamps are not set.
|
|
4498
4498
|
* @param {Function} [callback] params are (error, writeOpResult)
|
|
4499
4499
|
* @return {Query} this
|
|
4500
|
-
* @see Model.update #model_Model
|
|
4500
|
+
* @see Model.update #model_Model-update
|
|
4501
4501
|
* @see Query docs https://mongoosejs.com/docs/queries.html
|
|
4502
4502
|
* @see update https://docs.mongodb.org/manual/reference/method/db.collection.update/
|
|
4503
4503
|
* @see writeOpResult https://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#~WriteOpResult
|
|
@@ -4560,7 +4560,7 @@ Query.prototype.updateOne = function(conditions, doc, options, callback) {
|
|
|
4560
4560
|
* @param {Boolean} [options.timestamps=null] If set to `false` and [schema-level timestamps](/docs/guide.html#timestamps) are enabled, skip timestamps for this update. Does nothing if schema-level timestamps are not set.
|
|
4561
4561
|
* @param {Function} [callback] params are (error, writeOpResult)
|
|
4562
4562
|
* @return {Query} this
|
|
4563
|
-
* @see Model.update #model_Model
|
|
4563
|
+
* @see Model.update #model_Model-update
|
|
4564
4564
|
* @see Query docs https://mongoosejs.com/docs/queries.html
|
|
4565
4565
|
* @see update https://docs.mongodb.org/manual/reference/method/db.collection.update/
|
|
4566
4566
|
* @see writeOpResult https://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#~WriteOpResult
|
|
@@ -5061,7 +5061,7 @@ function castQuery(query) {
|
|
|
5061
5061
|
* @param {Object} [options.options=null] Additional options like `limit` and `lean`.
|
|
5062
5062
|
* @see population ./populate.html
|
|
5063
5063
|
* @see Query#select #query_Query-select
|
|
5064
|
-
* @see Model.populate #model_Model
|
|
5064
|
+
* @see Model.populate #model_Model-populate
|
|
5065
5065
|
* @return {Query} this
|
|
5066
5066
|
* @api public
|
|
5067
5067
|
*/
|
package/lib/schema.js
CHANGED
|
@@ -1698,7 +1698,7 @@ Schema.prototype.post = function(name) {
|
|
|
1698
1698
|
*
|
|
1699
1699
|
* @param {Function} plugin The Plugin's callback
|
|
1700
1700
|
* @param {Object} [opts] Options to pass to the plugin
|
|
1701
|
-
* @see plugins
|
|
1701
|
+
* @see plugins /docs/plugins.html
|
|
1702
1702
|
* @api public
|
|
1703
1703
|
*/
|
|
1704
1704
|
|
package/lib/schematype.js
CHANGED
|
@@ -134,6 +134,7 @@ SchemaType.prototype.OptionsConstructor = SchemaTypeOptions;
|
|
|
134
134
|
* The path to this SchemaType in a Schema.
|
|
135
135
|
*
|
|
136
136
|
* #### Example:
|
|
137
|
+
*
|
|
137
138
|
* const schema = new Schema({ name: String });
|
|
138
139
|
* schema.path('name').path; // 'name'
|
|
139
140
|
*
|
|
@@ -148,6 +149,7 @@ SchemaType.prototype.path;
|
|
|
148
149
|
* The validators that Mongoose should run to validate properties at this SchemaType's path.
|
|
149
150
|
*
|
|
150
151
|
* #### Example:
|
|
152
|
+
*
|
|
151
153
|
* const schema = new Schema({ name: { type: String, required: true } });
|
|
152
154
|
* schema.path('name').validators.length; // 1, the `required` validator
|
|
153
155
|
*
|
|
@@ -162,6 +164,7 @@ SchemaType.prototype.validators;
|
|
|
162
164
|
* True if this SchemaType has a required validator. False otherwise.
|
|
163
165
|
*
|
|
164
166
|
* #### Example:
|
|
167
|
+
*
|
|
165
168
|
* const schema = new Schema({ name: { type: String, required: true } });
|
|
166
169
|
* schema.path('name').isRequired; // true
|
|
167
170
|
*
|
|
@@ -175,8 +178,11 @@ SchemaType.prototype.validators;
|
|
|
175
178
|
|
|
176
179
|
SchemaType.prototype.validators;
|
|
177
180
|
|
|
178
|
-
|
|
179
|
-
*
|
|
181
|
+
/**
|
|
182
|
+
* Split the current dottet path into segments
|
|
183
|
+
*
|
|
184
|
+
* @return {String[]|undefined}
|
|
185
|
+
* @api private
|
|
180
186
|
*/
|
|
181
187
|
|
|
182
188
|
SchemaType.prototype.splitPath = function() {
|
|
@@ -351,8 +357,8 @@ SchemaType.get = function(getter) {
|
|
|
351
357
|
* const m2 = new M;
|
|
352
358
|
* console.log(m2.mixed); // { added: 1 }
|
|
353
359
|
*
|
|
354
|
-
* @param {Function|any} val
|
|
355
|
-
* @return {
|
|
360
|
+
* @param {Function|any} val The default value to set
|
|
361
|
+
* @return {Any|undefined} Returns the set default value.
|
|
356
362
|
* @api public
|
|
357
363
|
*/
|
|
358
364
|
|
|
@@ -414,7 +420,7 @@ SchemaType.prototype.index = function(options) {
|
|
|
414
420
|
*
|
|
415
421
|
* #### Example:
|
|
416
422
|
*
|
|
417
|
-
* const s = new Schema({ name: { type: String, unique: true }});
|
|
423
|
+
* const s = new Schema({ name: { type: String, unique: true } });
|
|
418
424
|
* s.path('name').index({ unique: true });
|
|
419
425
|
*
|
|
420
426
|
* _NOTE: violating the constraint returns an `E11000` error from MongoDB when saving, not a Mongoose validation error._
|
|
@@ -452,8 +458,9 @@ SchemaType.prototype.unique = function(bool) {
|
|
|
452
458
|
*
|
|
453
459
|
* ### Example:
|
|
454
460
|
*
|
|
455
|
-
* const s = new Schema({name : {type: String, text : true })
|
|
456
|
-
* s.path('name').index({text : true});
|
|
461
|
+
* const s = new Schema({ name : { type: String, text : true } })
|
|
462
|
+
* s.path('name').index({ text : true });
|
|
463
|
+
*
|
|
457
464
|
* @param {Boolean} bool
|
|
458
465
|
* @return {SchemaType} this
|
|
459
466
|
* @api public
|
|
@@ -462,7 +469,7 @@ SchemaType.prototype.unique = function(bool) {
|
|
|
462
469
|
SchemaType.prototype.text = function(bool) {
|
|
463
470
|
if (this._index === false) {
|
|
464
471
|
if (!bool) {
|
|
465
|
-
return;
|
|
472
|
+
return this;
|
|
466
473
|
}
|
|
467
474
|
throw new Error('Path "' + this.path + '" may not have `index` set to ' +
|
|
468
475
|
'false and `text` set to true');
|
|
@@ -499,7 +506,7 @@ SchemaType.prototype.text = function(bool) {
|
|
|
499
506
|
SchemaType.prototype.sparse = function(bool) {
|
|
500
507
|
if (this._index === false) {
|
|
501
508
|
if (!bool) {
|
|
502
|
-
return;
|
|
509
|
+
return this;
|
|
503
510
|
}
|
|
504
511
|
throw new Error('Path "' + this.path + '" may not have `index` set to ' +
|
|
505
512
|
'false and `sparse` set to true');
|
|
@@ -606,19 +613,17 @@ SchemaType.prototype.transform = function(fn) {
|
|
|
606
613
|
*
|
|
607
614
|
* #### Example:
|
|
608
615
|
*
|
|
609
|
-
*
|
|
610
|
-
*
|
|
611
|
-
*
|
|
612
|
-
*
|
|
613
|
-
* }
|
|
616
|
+
* function capitalize (val) {
|
|
617
|
+
* if (typeof val !== 'string') val = '';
|
|
618
|
+
* return val.charAt(0).toUpperCase() + val.substring(1);
|
|
619
|
+
* }
|
|
614
620
|
*
|
|
615
|
-
*
|
|
616
|
-
*
|
|
621
|
+
* // defining within the schema
|
|
622
|
+
* const s = new Schema({ name: { type: String, set: capitalize }});
|
|
617
623
|
*
|
|
618
|
-
*
|
|
619
|
-
*
|
|
620
|
-
*
|
|
621
|
-
* ```
|
|
624
|
+
* // or with the SchemaType
|
|
625
|
+
* const s = new Schema({ name: String })
|
|
626
|
+
* s.path('name').set(capitalize);
|
|
622
627
|
*
|
|
623
628
|
* Setters allow you to transform the data before it gets to the raw mongodb
|
|
624
629
|
* document or query.
|
|
@@ -630,76 +635,68 @@ SchemaType.prototype.transform = function(fn) {
|
|
|
630
635
|
*
|
|
631
636
|
* You can set up email lower case normalization easily via a Mongoose setter.
|
|
632
637
|
*
|
|
633
|
-
*
|
|
634
|
-
*
|
|
635
|
-
*
|
|
636
|
-
* }
|
|
638
|
+
* function toLower(v) {
|
|
639
|
+
* return v.toLowerCase();
|
|
640
|
+
* }
|
|
637
641
|
*
|
|
638
|
-
*
|
|
639
|
-
*
|
|
640
|
-
*
|
|
642
|
+
* const UserSchema = new Schema({
|
|
643
|
+
* email: { type: String, set: toLower }
|
|
644
|
+
* });
|
|
641
645
|
*
|
|
642
|
-
*
|
|
646
|
+
* const User = db.model('User', UserSchema);
|
|
643
647
|
*
|
|
644
|
-
*
|
|
645
|
-
*
|
|
648
|
+
* const user = new User({email: 'AVENUE@Q.COM'});
|
|
649
|
+
* console.log(user.email); // 'avenue@q.com'
|
|
646
650
|
*
|
|
647
|
-
*
|
|
648
|
-
*
|
|
649
|
-
*
|
|
650
|
-
*
|
|
651
|
-
*
|
|
652
|
-
* ```
|
|
651
|
+
* // or
|
|
652
|
+
* const user = new User();
|
|
653
|
+
* user.email = 'Avenue@Q.com';
|
|
654
|
+
* console.log(user.email); // 'avenue@q.com'
|
|
655
|
+
* User.updateOne({ _id: _id }, { $set: { email: 'AVENUE@Q.COM' } }); // update to 'avenue@q.com'
|
|
653
656
|
*
|
|
654
657
|
* As you can see above, setters allow you to transform the data before it
|
|
655
658
|
* stored in MongoDB, or before executing a query.
|
|
656
659
|
*
|
|
657
660
|
* _NOTE: we could have also just used the built-in `lowercase: true` SchemaType option instead of defining our own function._
|
|
658
661
|
*
|
|
659
|
-
*
|
|
660
|
-
* new Schema({ email: { type: String, lowercase: true }})
|
|
661
|
-
* ```
|
|
662
|
+
* new Schema({ email: { type: String, lowercase: true }})
|
|
662
663
|
*
|
|
663
664
|
* Setters are also passed a second argument, the schematype on which the setter was defined. This allows for tailored behavior based on options passed in the schema.
|
|
664
665
|
*
|
|
665
|
-
*
|
|
666
|
-
*
|
|
667
|
-
*
|
|
668
|
-
*
|
|
669
|
-
*
|
|
670
|
-
*
|
|
671
|
-
*
|
|
672
|
-
* }
|
|
666
|
+
* function inspector (val, priorValue, schematype) {
|
|
667
|
+
* if (schematype.options.required) {
|
|
668
|
+
* return schematype.path + ' is required';
|
|
669
|
+
* } else {
|
|
670
|
+
* return val;
|
|
671
|
+
* }
|
|
672
|
+
* }
|
|
673
673
|
*
|
|
674
|
-
*
|
|
675
|
-
*
|
|
676
|
-
*
|
|
677
|
-
*
|
|
674
|
+
* const VirusSchema = new Schema({
|
|
675
|
+
* name: { type: String, required: true, set: inspector },
|
|
676
|
+
* taxonomy: { type: String, set: inspector }
|
|
677
|
+
* })
|
|
678
678
|
*
|
|
679
|
-
*
|
|
680
|
-
*
|
|
679
|
+
* const Virus = db.model('Virus', VirusSchema);
|
|
680
|
+
* const v = new Virus({ name: 'Parvoviridae', taxonomy: 'Parvovirinae' });
|
|
681
681
|
*
|
|
682
|
-
*
|
|
683
|
-
*
|
|
684
|
-
* ```
|
|
682
|
+
* console.log(v.name); // name is required
|
|
683
|
+
* console.log(v.taxonomy); // Parvovirinae
|
|
685
684
|
*
|
|
686
685
|
* You can also use setters to modify other properties on the document. If
|
|
687
686
|
* you're setting a property `name` on a document, the setter will run with
|
|
688
687
|
* `this` as the document. Be careful, in mongoose 5 setters will also run
|
|
689
688
|
* when querying by `name` with `this` as the query.
|
|
690
689
|
*
|
|
691
|
-
*
|
|
692
|
-
*
|
|
693
|
-
*
|
|
694
|
-
*
|
|
695
|
-
*
|
|
696
|
-
*
|
|
697
|
-
*
|
|
698
|
-
*
|
|
699
|
-
*
|
|
700
|
-
*
|
|
701
|
-
* });
|
|
702
|
-
* ```
|
|
690
|
+
* const nameSchema = new Schema({ name: String, keywords: [String] });
|
|
691
|
+
* nameSchema.path('name').set(function(v) {
|
|
692
|
+
* // Need to check if `this` is a document, because in mongoose 5
|
|
693
|
+
* // setters will also run on queries, in which case `this` will be a
|
|
694
|
+
* // mongoose query object.
|
|
695
|
+
* if (this instanceof Document && v != null) {
|
|
696
|
+
* this.keywords = v.split(' ');
|
|
697
|
+
* }
|
|
698
|
+
* return v;
|
|
699
|
+
* });
|
|
703
700
|
*
|
|
704
701
|
* @param {Function} fn
|
|
705
702
|
* @return {SchemaType} this
|
|
@@ -824,7 +821,7 @@ SchemaType.prototype.get = function(fn) {
|
|
|
824
821
|
* From the examples above, you may have noticed that error messages support
|
|
825
822
|
* basic templating. There are a few other template keywords besides `{PATH}`
|
|
826
823
|
* and `{VALUE}` too. To find out more, details are available
|
|
827
|
-
* [here](#error_messages_MongooseError
|
|
824
|
+
* [here](#error_messages_MongooseError-messages).
|
|
828
825
|
*
|
|
829
826
|
* If Mongoose's built-in error message templating isn't enough, Mongoose
|
|
830
827
|
* supports setting the `message` property to a function.
|
|
@@ -993,9 +990,9 @@ SchemaType.prototype.validate = function(obj, message, type) {
|
|
|
993
990
|
* @param {String} [message] optional custom error message
|
|
994
991
|
* @return {SchemaType} this
|
|
995
992
|
* @see Customized Error Messages #error_messages_MongooseError-messages
|
|
996
|
-
* @see SchemaArray#checkRequired #schema_array_SchemaArray
|
|
993
|
+
* @see SchemaArray#checkRequired #schema_array_SchemaArray-checkRequired
|
|
997
994
|
* @see SchemaBoolean#checkRequired #schema_boolean_SchemaBoolean-checkRequired
|
|
998
|
-
* @see SchemaBuffer#checkRequired #schema_buffer_SchemaBuffer
|
|
995
|
+
* @see SchemaBuffer#checkRequired #schema_buffer_SchemaBuffer-schemaName
|
|
999
996
|
* @see SchemaNumber#checkRequired #schema_number_SchemaNumber-min
|
|
1000
997
|
* @see SchemaObjectId#checkRequired #schema_objectid_ObjectId-auto
|
|
1001
998
|
* @see SchemaString#checkRequired #schema_string_SchemaString-checkRequired
|
|
@@ -1079,6 +1076,7 @@ SchemaType.prototype.required = function(required, message) {
|
|
|
1079
1076
|
* looks at to determine the foreign collection it should query.
|
|
1080
1077
|
*
|
|
1081
1078
|
* #### Example:
|
|
1079
|
+
*
|
|
1082
1080
|
* const userSchema = new Schema({ name: String });
|
|
1083
1081
|
* const User = mongoose.model('User', userSchema);
|
|
1084
1082
|
*
|
|
@@ -1108,6 +1106,7 @@ SchemaType.prototype.ref = function(ref) {
|
|
|
1108
1106
|
*
|
|
1109
1107
|
* @param {Object} scope the scope which callback are executed
|
|
1110
1108
|
* @param {Boolean} init
|
|
1109
|
+
* @return {Any} The Stored default value.
|
|
1111
1110
|
* @api private
|
|
1112
1111
|
*/
|
|
1113
1112
|
|
|
@@ -1175,6 +1174,7 @@ SchemaType.prototype._castNullish = function _castNullish(v) {
|
|
|
1175
1174
|
* @param {Object} value
|
|
1176
1175
|
* @param {Object} scope
|
|
1177
1176
|
* @param {Boolean} init
|
|
1177
|
+
* @return {Any}
|
|
1178
1178
|
* @api private
|
|
1179
1179
|
*/
|
|
1180
1180
|
|
|
@@ -1195,6 +1195,7 @@ SchemaType.prototype.applySetters = function(value, scope, init, priorVal, optio
|
|
|
1195
1195
|
*
|
|
1196
1196
|
* @param {Object} value
|
|
1197
1197
|
* @param {Object} scope
|
|
1198
|
+
* @return {Any}
|
|
1198
1199
|
* @api private
|
|
1199
1200
|
*/
|
|
1200
1201
|
|
|
@@ -1239,9 +1240,12 @@ SchemaType.prototype.select = function select(val) {
|
|
|
1239
1240
|
/**
|
|
1240
1241
|
* Performs a validation of `value` using the validators declared for this SchemaType.
|
|
1241
1242
|
*
|
|
1242
|
-
* @param {
|
|
1243
|
+
* @param {Any} value
|
|
1243
1244
|
* @param {Function} callback
|
|
1244
1245
|
* @param {Object} scope
|
|
1246
|
+
* @param {Object} [options]
|
|
1247
|
+
* @param {String} [options.path]
|
|
1248
|
+
* @return {Any} If no validators, returns the output from calling `fn`, otherwise no return
|
|
1245
1249
|
* @api public
|
|
1246
1250
|
*/
|
|
1247
1251
|
|
|
@@ -1352,9 +1356,11 @@ function _validate(ok, validatorProperties) {
|
|
|
1352
1356
|
*
|
|
1353
1357
|
* This method ignores the asynchronous validators.
|
|
1354
1358
|
*
|
|
1355
|
-
* @param {
|
|
1359
|
+
* @param {Any} value
|
|
1356
1360
|
* @param {Object} scope
|
|
1357
|
-
* @
|
|
1361
|
+
* @param {Object} [options]
|
|
1362
|
+
* @param {Object} [options.path]
|
|
1363
|
+
* @return {MongooseError|null}
|
|
1358
1364
|
* @api private
|
|
1359
1365
|
*/
|
|
1360
1366
|
|
|
@@ -1595,7 +1601,8 @@ SchemaType.prototype.castForQueryWrapper = function(params) {
|
|
|
1595
1601
|
* Cast the given value with the given optional query operator.
|
|
1596
1602
|
*
|
|
1597
1603
|
* @param {String} [$conditional] query operator, like `$eq` or `$in`
|
|
1598
|
-
* @param {
|
|
1604
|
+
* @param {Any} val
|
|
1605
|
+
* @return {Any}
|
|
1599
1606
|
* @api private
|
|
1600
1607
|
*/
|
|
1601
1608
|
|
|
@@ -1612,9 +1619,11 @@ SchemaType.prototype.castForQuery = function($conditional, val) {
|
|
|
1612
1619
|
return this._castForQuery(val);
|
|
1613
1620
|
};
|
|
1614
1621
|
|
|
1615
|
-
|
|
1622
|
+
/**
|
|
1616
1623
|
* Internal switch for runSetters
|
|
1617
1624
|
*
|
|
1625
|
+
* @param {Any} val
|
|
1626
|
+
* @return {Any}
|
|
1618
1627
|
* @api private
|
|
1619
1628
|
*/
|
|
1620
1629
|
|
|
@@ -1623,6 +1632,7 @@ SchemaType.prototype._castForQuery = function(val) {
|
|
|
1623
1632
|
};
|
|
1624
1633
|
|
|
1625
1634
|
/**
|
|
1635
|
+
* Set & Get the `checkRequired` function
|
|
1626
1636
|
* Override the function the required validator uses to check whether a value
|
|
1627
1637
|
* passes the `required` check. Override this on the individual SchemaType.
|
|
1628
1638
|
*
|
|
@@ -1631,8 +1641,8 @@ SchemaType.prototype._castForQuery = function(val) {
|
|
|
1631
1641
|
* // Use this to allow empty strings to pass the `required` validator
|
|
1632
1642
|
* mongoose.Schema.Types.String.checkRequired(v => typeof v === 'string');
|
|
1633
1643
|
*
|
|
1634
|
-
* @param {Function} fn
|
|
1635
|
-
* @return {Function}
|
|
1644
|
+
* @param {Function} [fn] If set, will overwrite the current set function
|
|
1645
|
+
* @return {Function} The input `fn` or the already set function
|
|
1636
1646
|
* @static
|
|
1637
1647
|
* @memberOf SchemaType
|
|
1638
1648
|
* @function checkRequired
|
|
@@ -1650,7 +1660,8 @@ SchemaType.checkRequired = function(fn) {
|
|
|
1650
1660
|
/**
|
|
1651
1661
|
* Default check for if this path satisfies the `required` validator.
|
|
1652
1662
|
*
|
|
1653
|
-
* @param {
|
|
1663
|
+
* @param {Any} val
|
|
1664
|
+
* @return {Boolean} `true` when the value is not `null`, `false` otherwise
|
|
1654
1665
|
* @api private
|
|
1655
1666
|
*/
|
|
1656
1667
|
|
|
@@ -1658,8 +1669,11 @@ SchemaType.prototype.checkRequired = function(val) {
|
|
|
1658
1669
|
return val != null;
|
|
1659
1670
|
};
|
|
1660
1671
|
|
|
1661
|
-
|
|
1662
|
-
*
|
|
1672
|
+
/**
|
|
1673
|
+
* Clone the current SchemaType
|
|
1674
|
+
*
|
|
1675
|
+
* @return {SchemaType} The cloned SchemaType instance
|
|
1676
|
+
* @api private
|
|
1663
1677
|
*/
|
|
1664
1678
|
|
|
1665
1679
|
SchemaType.prototype.clone = function() {
|