mongoose 9.2.4 → 9.3.0
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 +27 -4
- package/lib/connection.js +10 -24
- package/lib/cursor/aggregationCursor.js +2 -2
- package/lib/cursor/changeStream.js +55 -35
- package/lib/cursor/queryCursor.js +2 -2
- package/lib/document.js +218 -205
- package/lib/drivers/node-mongodb-native/collection.js +1 -1
- package/lib/helpers/query/getEmbeddedDiscriminatorPath.js +1 -1
- package/lib/helpers/setDefaultsOnInsert.js +7 -5
- package/lib/helpers/update/removeUnusedArrayFilters.js +7 -2
- package/lib/helpers/updateValidators.js +1 -8
- package/lib/model.js +69 -44
- package/lib/mongoose.js +4 -6
- package/lib/options/schemaDocumentArrayOptions.js +23 -0
- package/lib/options/schemaSubdocumentOptions.js +23 -0
- package/lib/plugins/index.js +0 -1
- package/lib/query.js +133 -94
- package/lib/schema/buffer.js +1 -1
- package/lib/schema/documentArray.js +1 -1
- package/lib/schema.js +2 -2
- package/lib/schemaType.js +15 -7
- package/package.json +5 -4
- package/types/aggregate.d.ts +3 -0
- package/types/connection.d.ts +2 -2
- package/types/cursor.d.ts +1 -1
- package/types/document.d.ts +4 -4
- package/types/index.d.ts +1 -1
- package/types/indexes.d.ts +1 -1
- package/types/inferhydrateddoctype.d.ts +23 -3
- package/types/inferrawdoctype.d.ts +32 -10
- package/types/inferschematype.d.ts +31 -4
- package/types/pipelinestage.d.ts +3 -2
- package/types/query.d.ts +2 -2
- package/types/schematypes.d.ts +3 -0
- package/lib/plugins/validateBeforeSave.js +0 -47
package/lib/model.js
CHANGED
|
@@ -373,6 +373,23 @@ function _createSaveOptions(doc, options) {
|
|
|
373
373
|
|
|
374
374
|
Model.prototype.$__save = async function $__save(options) {
|
|
375
375
|
try {
|
|
376
|
+
const hasValidateBeforeSaveOption = options &&
|
|
377
|
+
(typeof options === 'object') &&
|
|
378
|
+
('validateBeforeSave' in options);
|
|
379
|
+
const shouldValidateBeforeSave = hasValidateBeforeSaveOption ?
|
|
380
|
+
!!options.validateBeforeSave :
|
|
381
|
+
this.$__schema.options.validateBeforeSave;
|
|
382
|
+
if (shouldValidateBeforeSave) {
|
|
383
|
+
const hasValidateModifiedOnlyOption = options != null &&
|
|
384
|
+
typeof options === 'object' &&
|
|
385
|
+
Object.hasOwn(options, 'validateModifiedOnly');
|
|
386
|
+
const validateOptions = hasValidateModifiedOnlyOption ?
|
|
387
|
+
{ validateModifiedOnly: options.validateModifiedOnly } :
|
|
388
|
+
null;
|
|
389
|
+
await this.$validate(validateOptions);
|
|
390
|
+
this.$op = 'save';
|
|
391
|
+
}
|
|
392
|
+
|
|
376
393
|
await this._execDocumentPreHooks('save', options, [options]);
|
|
377
394
|
} catch (error) {
|
|
378
395
|
await this._execDocumentPostHooks('save', options, error);
|
|
@@ -387,7 +404,9 @@ Model.prototype.$__save = async function $__save(options) {
|
|
|
387
404
|
|
|
388
405
|
if (this.$isNew) {
|
|
389
406
|
// send entire doc
|
|
390
|
-
const obj = this
|
|
407
|
+
const obj = this.$__hasOnlyPrimitiveValues() ?
|
|
408
|
+
this.$__toObjectShallow() :
|
|
409
|
+
this.toObject(saveToObjectOptions);
|
|
391
410
|
if ((obj || {})._id === void 0) {
|
|
392
411
|
// documents must have an _id else mongoose won't know
|
|
393
412
|
// what to update later if more changes are made. the user
|
|
@@ -625,11 +644,13 @@ Model.prototype.save = async function save(options) {
|
|
|
625
644
|
if (this.$__.timestamps != null) {
|
|
626
645
|
options.timestamps = this.$__.timestamps;
|
|
627
646
|
}
|
|
628
|
-
this.$
|
|
629
|
-
this
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
647
|
+
if (!this.$isNew) {
|
|
648
|
+
this.$__.$versionError = generateVersionError(
|
|
649
|
+
this,
|
|
650
|
+
this.modifiedPaths(),
|
|
651
|
+
Object.keys(this.$__.activePaths.getStatePaths('default'))
|
|
652
|
+
);
|
|
653
|
+
}
|
|
633
654
|
|
|
634
655
|
if (parallelSave) {
|
|
635
656
|
this.$__handleReject(parallelSave);
|
|
@@ -1172,7 +1193,7 @@ Model.init = function init() {
|
|
|
1172
1193
|
* });
|
|
1173
1194
|
*
|
|
1174
1195
|
* @api public
|
|
1175
|
-
* @param {object} [options] see [MongoDB driver docs](https://mongodb.github.io/node-mongodb-native/
|
|
1196
|
+
* @param {object} [options] see [MongoDB driver docs](https://mongodb.github.io/node-mongodb-native/7.0/classes/Db.html#createCollection)
|
|
1176
1197
|
* @returns {Promise}
|
|
1177
1198
|
*/
|
|
1178
1199
|
|
|
@@ -1629,7 +1650,7 @@ Model.ensureIndexes = async function ensureIndexes(options) {
|
|
|
1629
1650
|
};
|
|
1630
1651
|
|
|
1631
1652
|
/**
|
|
1632
|
-
* Similar to `ensureIndexes()`, except for it uses the [`createIndex`](https://mongodb.github.io/node-mongodb-native/
|
|
1653
|
+
* Similar to `ensureIndexes()`, except for it uses the [`createIndex`](https://mongodb.github.io/node-mongodb-native/7.0/classes/Db.html#createIndex)
|
|
1633
1654
|
* function.
|
|
1634
1655
|
*
|
|
1635
1656
|
* @param {object} [options] internal options
|
|
@@ -2206,7 +2227,7 @@ Model.estimatedDocumentCount = function estimatedDocumentCount(options) {
|
|
|
2206
2227
|
* a full collection scan and **not** use any indexes.
|
|
2207
2228
|
*
|
|
2208
2229
|
* The `countDocuments()` function is similar to `count()`, but there are a
|
|
2209
|
-
* [few operators that `countDocuments()` does not support](https://mongodb.github.io/node-mongodb-native/
|
|
2230
|
+
* [few operators that `countDocuments()` does not support](https://mongodb.github.io/node-mongodb-native/7.0/classes/Collection.html#countDocuments).
|
|
2210
2231
|
* Below are the operators that `count()` supports but `countDocuments()` does not,
|
|
2211
2232
|
* and the suggested replacement:
|
|
2212
2233
|
*
|
|
@@ -2373,7 +2394,7 @@ Model.$where = function $where() {
|
|
|
2373
2394
|
* @param {object|string} [options.sort] if multiple docs are found by the conditions, sets the sort order to choose which doc to update.
|
|
2374
2395
|
* @param {boolean} [options.runValidators] if true, runs [update validators](https://mongoosejs.com/docs/validation.html#update-validators) on this command. Update validators validate the update operation against the model's schema
|
|
2375
2396
|
* @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
|
|
2376
|
-
* @param {boolean} [options.includeResultMetadata] if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/
|
|
2397
|
+
* @param {boolean} [options.includeResultMetadata] if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/7.0/interfaces/ModifyResult.html)
|
|
2377
2398
|
* @param {boolean} [options.translateAliases=null] If set to `true`, translates any schema-defined aliases in `filter`, `projection`, `update`, and `distinct`. Throws an error if there are any conflicts where both alias and raw property are defined on the same object.
|
|
2378
2399
|
* @param {boolean} [options.overwriteDiscriminatorKey=false] Mongoose removes discriminator key updates from `update` by default, set `overwriteDiscriminatorKey` to `true` to allow updating the discriminator key
|
|
2379
2400
|
* @return {Query}
|
|
@@ -2457,7 +2478,7 @@ Model.findOneAndUpdate = function(conditions, update, options) {
|
|
|
2457
2478
|
* @param {object|string} [options.sort] if multiple docs are found by the conditions, sets the sort order to choose which doc to update.
|
|
2458
2479
|
* @param {boolean} [options.runValidators] if true, runs [update validators](https://mongoosejs.com/docs/validation.html#update-validators) on this command. Update validators validate the update operation against the model's schema
|
|
2459
2480
|
* @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
|
|
2460
|
-
* @param {boolean} [options.includeResultMetadata] if true, returns the full [ModifyResult from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/
|
|
2481
|
+
* @param {boolean} [options.includeResultMetadata] if true, returns the full [ModifyResult from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/7.0/interfaces/ModifyResult.html) rather than just the document
|
|
2461
2482
|
* @param {boolean} [options.upsert=false] if true, and no documents found, insert a new document
|
|
2462
2483
|
* @param {boolean} [options.new=false] if true, return the modified document rather than the original
|
|
2463
2484
|
* @param {object|string} [options.select] sets the document fields to return.
|
|
@@ -2513,7 +2534,7 @@ Model.findByIdAndUpdate = function(id, update, options) {
|
|
|
2513
2534
|
* @param {boolean|'throw'} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)
|
|
2514
2535
|
* @param {object|string|string[]} [options.projection=null] optional fields to return, see [`Query.prototype.select()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.select())
|
|
2515
2536
|
* @param {ClientSession} [options.session=null] The session associated with this query. See [transactions docs](https://mongoosejs.com/docs/transactions.html).
|
|
2516
|
-
* @param {boolean} [options.includeResultMetadata] if true, returns the full [ModifyResult from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/
|
|
2537
|
+
* @param {boolean} [options.includeResultMetadata] if true, returns the full [ModifyResult from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/7.0/interfaces/ModifyResult.html) rather than just the document
|
|
2517
2538
|
* @param {object|string} [options.sort] if multiple docs are found by the conditions, sets the sort order to choose which doc to update.
|
|
2518
2539
|
* @param {object|string} [options.select] sets the document fields to return.
|
|
2519
2540
|
* @param {number} [options.maxTimeMS] puts a time limit on the query - requires mongodb >= 2.6.0
|
|
@@ -2594,7 +2615,7 @@ Model.findByIdAndDelete = function(id, options) {
|
|
|
2594
2615
|
* @param {boolean} [options.timestamps=null] If set to `false` and [schema-level timestamps](https://mongoosejs.com/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.
|
|
2595
2616
|
* @param {object|string|string[]} [options.projection=null] optional fields to return, see [`Query.prototype.select()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.select())
|
|
2596
2617
|
* @param {object|string} [options.sort] if multiple docs are found by the conditions, sets the sort order to choose which doc to update.
|
|
2597
|
-
* @param {boolean} [options.includeResultMetadata] if true, returns the full [ModifyResult from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/
|
|
2618
|
+
* @param {boolean} [options.includeResultMetadata] if true, returns the full [ModifyResult from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/7.0/interfaces/ModifyResult.html) rather than just the document
|
|
2598
2619
|
* @param {object|string} [options.select] sets the document fields to return.
|
|
2599
2620
|
* @param {number} [options.maxTimeMS] puts a time limit on the query - requires mongodb >= 2.6.0
|
|
2600
2621
|
* @param {boolean} [options.translateAliases=null] If set to `true`, translates any schema-defined aliases in `filter`, `projection`, `update`, and `distinct`. Throws an error if there are any conflicts where both alias and raw property are defined on the same object.
|
|
@@ -2711,6 +2732,26 @@ Model.create = async function create(doc, options) {
|
|
|
2711
2732
|
throw new MongooseError('Cannot call `create()` with a session and multiple documents unless `ordered: true` is set');
|
|
2712
2733
|
}
|
|
2713
2734
|
|
|
2735
|
+
if (!Array.isArray(doc) && args.length === 1) {
|
|
2736
|
+
let toSave = doc;
|
|
2737
|
+
|
|
2738
|
+
const Model = this.discriminators && doc[discriminatorKey] != null ?
|
|
2739
|
+
this.discriminators[doc[discriminatorKey]] || getDiscriminatorByValue(this.discriminators, doc[discriminatorKey]) :
|
|
2740
|
+
this;
|
|
2741
|
+
if (Model == null) {
|
|
2742
|
+
throw new MongooseError(`Discriminator "${doc[discriminatorKey]}" not ` +
|
|
2743
|
+
`found for model "${this.modelName}"`);
|
|
2744
|
+
}
|
|
2745
|
+
|
|
2746
|
+
if (!(toSave instanceof Model)) {
|
|
2747
|
+
toSave = new Model(toSave);
|
|
2748
|
+
}
|
|
2749
|
+
|
|
2750
|
+
await toSave.$save(options);
|
|
2751
|
+
|
|
2752
|
+
return toSave;
|
|
2753
|
+
}
|
|
2754
|
+
|
|
2714
2755
|
if (options.ordered) {
|
|
2715
2756
|
for (let i = 0; i < args.length; i++) {
|
|
2716
2757
|
try {
|
|
@@ -2789,11 +2830,6 @@ Model.create = async function create(doc, options) {
|
|
|
2789
2830
|
}
|
|
2790
2831
|
}
|
|
2791
2832
|
|
|
2792
|
-
|
|
2793
|
-
if (!Array.isArray(doc) && args.length === 1) {
|
|
2794
|
-
return res[0];
|
|
2795
|
-
}
|
|
2796
|
-
|
|
2797
2833
|
return res;
|
|
2798
2834
|
};
|
|
2799
2835
|
|
|
@@ -2867,7 +2903,7 @@ Model.insertOne = async function insertOne(doc, options) {
|
|
|
2867
2903
|
* await doc.deleteOne();
|
|
2868
2904
|
*
|
|
2869
2905
|
* @param {Array} [pipeline]
|
|
2870
|
-
* @param {object} [options] see the [mongodb driver options](https://mongodb.github.io/node-mongodb-native/
|
|
2906
|
+
* @param {object} [options] see the [mongodb driver options](https://mongodb.github.io/node-mongodb-native/7.0/classes/Collection.html#watch)
|
|
2871
2907
|
* @param {boolean} [options.hydrate=false] if true and `fullDocument: 'updateLookup'` is set, Mongoose will automatically hydrate `fullDocument` into a fully fledged Mongoose document
|
|
2872
2908
|
* @return {ChangeStream} mongoose-specific change stream wrapper, inherits from EventEmitter
|
|
2873
2909
|
* @api public
|
|
@@ -2883,24 +2919,14 @@ Model.watch = function(pipeline, options) {
|
|
|
2883
2919
|
options.model = this;
|
|
2884
2920
|
|
|
2885
2921
|
|
|
2886
|
-
|
|
2887
|
-
|
|
2888
|
-
prepareDiscriminatorPipeline(pipeline, this.schema, 'fullDocument');
|
|
2889
|
-
if (this.$__collection.buffer) {
|
|
2890
|
-
this.$__collection.addQueue(() => {
|
|
2891
|
-
if (this.closed) {
|
|
2892
|
-
return;
|
|
2893
|
-
}
|
|
2894
|
-
const driverChangeStream = this.$__collection.watch(pipeline, watchOptions);
|
|
2895
|
-
cb(null, driverChangeStream);
|
|
2896
|
-
});
|
|
2897
|
-
} else {
|
|
2898
|
-
const driverChangeStream = this.$__collection.watch(pipeline, watchOptions);
|
|
2899
|
-
cb(null, driverChangeStream);
|
|
2900
|
-
}
|
|
2901
|
-
};
|
|
2922
|
+
pipeline = pipeline || [];
|
|
2923
|
+
prepareDiscriminatorPipeline(pipeline, this.schema, 'fullDocument');
|
|
2902
2924
|
|
|
2903
|
-
|
|
2925
|
+
const changeStreamPromise = this.db._waitForConnect().then(
|
|
2926
|
+
() => this.$__collection.watch(pipeline, watchOptions)
|
|
2927
|
+
);
|
|
2928
|
+
|
|
2929
|
+
return new ChangeStream(changeStreamPromise, pipeline, options);
|
|
2904
2930
|
};
|
|
2905
2931
|
|
|
2906
2932
|
/**
|
|
@@ -2923,7 +2949,7 @@ Model.watch = function(pipeline, options) {
|
|
|
2923
2949
|
* // secondary that is experiencing replication lag.
|
|
2924
2950
|
* doc = await Person.findOne({ name: 'Ned Stark' }, null, { session, readPreference: 'secondary' });
|
|
2925
2951
|
*
|
|
2926
|
-
* @param {object} [options] see the [mongodb driver options](https://mongodb.github.io/node-mongodb-native/
|
|
2952
|
+
* @param {object} [options] see the [mongodb driver options](https://mongodb.github.io/node-mongodb-native/7.0/classes/MongoClient.html#startSession)
|
|
2927
2953
|
* @param {boolean} [options.causalConsistency=true] set to false to disable causal consistency
|
|
2928
2954
|
* @return {Promise<ClientSession>} promise that resolves to a MongoDB driver `ClientSession`
|
|
2929
2955
|
* @api public
|
|
@@ -2967,9 +2993,9 @@ Model.startSession = function() {
|
|
|
2967
2993
|
* ], { rawResult: true });
|
|
2968
2994
|
*
|
|
2969
2995
|
* @param {Array|object|any} doc(s)
|
|
2970
|
-
* @param {object} [options] see the [mongodb driver options](https://mongodb.github.io/node-mongodb-native/
|
|
2996
|
+
* @param {object} [options] see the [mongodb driver options](https://mongodb.github.io/node-mongodb-native/7.0/classes/Collection.html#insertMany)
|
|
2971
2997
|
* @param {boolean} [options.ordered=true] if true, will fail fast on the first error encountered. If false, will insert all the documents it can and report errors later. An `insertMany()` with `ordered = false` is called an "unordered" `insertMany()`.
|
|
2972
|
-
* @param {boolean} [options.rawResult=false] if false, the returned promise resolves to the documents that passed mongoose document validation. If `true`, will return the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/
|
|
2998
|
+
* @param {boolean} [options.rawResult=false] if false, the returned promise resolves to the documents that passed mongoose document validation. If `true`, will return the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/7.0/interfaces/InsertManyResult.html) with a `mongoose` property that contains `validationErrors` and `results` if this is an unordered `insertMany`.
|
|
2973
2999
|
* @param {boolean} [options.lean=false] if `true`, skips hydrating the documents. This means Mongoose will **not** cast, validate, or apply defaults to any of the documents passed to `insertMany()`. This option is useful if you need the extra performance, but comes with data integrity risk. Consider using with [`castObject()`](https://mongoosejs.com/docs/api/model.html#Model.castObject()) and [`applyDefaults()`](https://mongoosejs.com/docs/api/model.html#Model.applyDefaults()).
|
|
2974
3000
|
* @param {number} [options.limit=null] this limits the number of documents being processed (validation/casting) by mongoose in parallel, this does **NOT** send the documents in batches to MongoDB. Use this option if you're processing a large number of documents and your app is running out of memory.
|
|
2975
3001
|
* @param {string|object|Array} [options.populate=null] populates the result documents. This option is a no-op if `rawResult` is set.
|
|
@@ -3340,7 +3366,7 @@ function _setIsNew(doc, val) {
|
|
|
3340
3366
|
* @param {boolean|object} [options.middleware=true] set to `false` to skip all user-defined middleware
|
|
3341
3367
|
* @param {boolean} [options.middleware.pre=true] set to `false` to skip only pre hooks
|
|
3342
3368
|
* @param {boolean} [options.middleware.post=true] set to `false` to skip only post hooks
|
|
3343
|
-
* @return {Promise} resolves to a [`BulkWriteOpResult`](https://mongodb.github.io/node-mongodb-native/
|
|
3369
|
+
* @return {Promise} resolves to a [`BulkWriteOpResult`](https://mongodb.github.io/node-mongodb-native/7.0/classes/BulkWriteResult.html) if the operation succeeds
|
|
3344
3370
|
* @api public
|
|
3345
3371
|
*/
|
|
3346
3372
|
|
|
@@ -3964,7 +3990,7 @@ Model.hydrate = function(obj, projection, options) {
|
|
|
3964
3990
|
* @return {Query}
|
|
3965
3991
|
* @see Query docs https://mongoosejs.com/docs/queries.html
|
|
3966
3992
|
* @see MongoDB docs https://www.mongodb.com/docs/manual/reference/command/update/#update-command-output
|
|
3967
|
-
* @see UpdateResult https://mongodb.github.io/node-mongodb-native/
|
|
3993
|
+
* @see UpdateResult https://mongodb.github.io/node-mongodb-native/7.0/interfaces/UpdateResult.html
|
|
3968
3994
|
* @api public
|
|
3969
3995
|
*/
|
|
3970
3996
|
|
|
@@ -4012,7 +4038,7 @@ Model.updateMany = function updateMany(conditions, update, options) {
|
|
|
4012
4038
|
* @return {Query}
|
|
4013
4039
|
* @see Query docs https://mongoosejs.com/docs/queries.html
|
|
4014
4040
|
* @see MongoDB docs https://www.mongodb.com/docs/manual/reference/command/update/#update-command-output
|
|
4015
|
-
* @see UpdateResult https://mongodb.github.io/node-mongodb-native/
|
|
4041
|
+
* @see UpdateResult https://mongodb.github.io/node-mongodb-native/7.0/interfaces/UpdateResult.html
|
|
4016
4042
|
* @api public
|
|
4017
4043
|
*/
|
|
4018
4044
|
|
|
@@ -4048,7 +4074,7 @@ Model.updateOne = function updateOne(conditions, doc, options) {
|
|
|
4048
4074
|
* @param {boolean} [options.translateAliases=null] If set to `true`, translates any schema-defined aliases in `filter`, `projection`, `update`, and `distinct`. Throws an error if there are any conflicts where both alias and raw property are defined on the same object.
|
|
4049
4075
|
* @return {Query}
|
|
4050
4076
|
* @see Query docs https://mongoosejs.com/docs/queries.html
|
|
4051
|
-
* @see UpdateResult https://mongodb.github.io/node-mongodb-native/
|
|
4077
|
+
* @see UpdateResult https://mongodb.github.io/node-mongodb-native/7.0/interfaces/UpdateResult.html
|
|
4052
4078
|
* @return {Query}
|
|
4053
4079
|
* @api public
|
|
4054
4080
|
*/
|
|
@@ -4866,7 +4892,6 @@ Model.compile = function compile(name, schema, collectionName, connection, base)
|
|
|
4866
4892
|
model.events = new EventEmitter();
|
|
4867
4893
|
|
|
4868
4894
|
schema._preCompile();
|
|
4869
|
-
|
|
4870
4895
|
const _userProvidedOptions = schema._userProvidedOptions || {};
|
|
4871
4896
|
|
|
4872
4897
|
const collectionOptions = {
|
package/lib/mongoose.js
CHANGED
|
@@ -386,13 +386,12 @@ Mongoose.prototype.get = Mongoose.prototype.set;
|
|
|
386
386
|
* await db.openUri('mongodb://127.0.0.1:27017/database');
|
|
387
387
|
*
|
|
388
388
|
* @param {string} uri mongodb URI to connect to
|
|
389
|
-
* @param {object} [options] passed down to the [MongoDB driver's `connect()` function](https://mongodb.github.io/node-mongodb-native/
|
|
389
|
+
* @param {object} [options] passed down to the [MongoDB driver's `connect()` function](https://mongodb.github.io/node-mongodb-native/7.0/interfaces/MongoClientOptions.html), except for 4 mongoose-specific options explained below.
|
|
390
390
|
* @param {boolean} [options.bufferCommands=true] Mongoose specific option. Set to false to [disable buffering](https://mongoosejs.com/docs/faq.html#callback_never_executes) on all models associated with this connection.
|
|
391
391
|
* @param {string} [options.dbName] The name of the database you want to use. If not provided, Mongoose uses the database name from connection string.
|
|
392
392
|
* @param {string} [options.user] username for authentication, equivalent to `options.auth.username`. Maintained for backwards compatibility.
|
|
393
393
|
* @param {string} [options.pass] password for authentication, equivalent to `options.auth.password`. Maintained for backwards compatibility.
|
|
394
394
|
* @param {boolean} [options.autoIndex=true] Mongoose-specific option. Set to false to disable automatic index creation for all models associated with this connection.
|
|
395
|
-
* @param {Class} [options.promiseLibrary] Sets the [underlying driver's promise library](https://mongodb.github.io/node-mongodb-native/4.9/interfaces/MongoClientOptions.html#promiseLibrary).
|
|
396
395
|
* @param {number} [options.maxPoolSize=5] The maximum number of sockets the MongoDB driver will keep open for this connection. Keep in mind that MongoDB only allows one operation per socket at a time, so you may want to increase this if you find you have a few slow queries that are blocking faster queries from proceeding. See [Slow Trains in MongoDB and Node.js](https://thecodebarbarian.com/slow-trains-in-mongodb-and-nodejs).
|
|
397
396
|
* @param {number} [options.minPoolSize=1] The minimum number of sockets the MongoDB driver will keep open for this connection. Keep in mind that MongoDB only allows one operation per socket at a time, so you may want to increase this if you find you have a few slow queries that are blocking faster queries from proceeding. See [Slow Trains in MongoDB and Node.js](https://thecodebarbarian.com/slow-trains-in-mongodb-and-nodejs).
|
|
398
397
|
* @param {number} [options.socketTimeoutMS=0] How long the MongoDB driver will wait before killing a socket due to inactivity _after initial connection_. Defaults to 0, which means Node.js will not time out the socket due to inactivity. A socket may be inactive because of either no activity or a long-running operation. This option is passed to [Node.js `socket#setTimeout()` function](https://nodejs.org/api/net.html#net_socket_settimeout_timeout_callback) after the MongoDB driver successfully completes.
|
|
@@ -437,7 +436,7 @@ Mongoose.prototype.createConnection = function createConnection(uri, options) {
|
|
|
437
436
|
* await mongoose.connect(uri);
|
|
438
437
|
*
|
|
439
438
|
* @param {string} uri mongodb URI to connect to
|
|
440
|
-
* @param {object} [options] passed down to the [MongoDB driver's `connect()` function](https://mongodb.github.io/node-mongodb-native/
|
|
439
|
+
* @param {object} [options] passed down to the [MongoDB driver's `connect()` function](https://mongodb.github.io/node-mongodb-native/7.0/interfaces/MongoClientOptions.html), except for 4 mongoose-specific options explained below.
|
|
441
440
|
* @param {boolean} [options.bufferCommands=true] Mongoose specific option. Set to false to [disable buffering](https://mongoosejs.com/docs/faq.html#callback_never_executes) on all models associated with this connection.
|
|
442
441
|
* @param {number} [options.bufferTimeoutMS=10000] Mongoose specific option. If `bufferCommands` is true, Mongoose will throw an error after `bufferTimeoutMS` if the operation is still buffered.
|
|
443
442
|
* @param {string} [options.dbName] The name of the database we want to use. If not provided, use database name from connection string.
|
|
@@ -448,7 +447,6 @@ Mongoose.prototype.createConnection = function createConnection(uri, options) {
|
|
|
448
447
|
* @param {number} [options.serverSelectionTimeoutMS] If `useUnifiedTopology = true`, the MongoDB driver will try to find a server to send any given operation to, and keep retrying for `serverSelectionTimeoutMS` milliseconds before erroring out. If not set, the MongoDB driver defaults to using `30000` (30 seconds).
|
|
449
448
|
* @param {number} [options.heartbeatFrequencyMS] If `useUnifiedTopology = true`, the MongoDB driver sends a heartbeat every `heartbeatFrequencyMS` to check on the status of the connection. A heartbeat is subject to `serverSelectionTimeoutMS`, so the MongoDB driver will retry failed heartbeats for up to 30 seconds by default. Mongoose only emits a `'disconnected'` event after a heartbeat has failed, so you may want to decrease this setting to reduce the time between when your server goes down and when Mongoose emits `'disconnected'`. We recommend you do **not** set this setting below 1000, too many heartbeats can lead to performance degradation.
|
|
450
449
|
* @param {boolean} [options.autoIndex=true] Mongoose-specific option. Set to false to disable automatic index creation for all models associated with this connection.
|
|
451
|
-
* @param {Class} [options.promiseLibrary] Sets the [underlying driver's promise library](https://mongodb.github.io/node-mongodb-native/4.9/interfaces/MongoClientOptions.html#promiseLibrary).
|
|
452
450
|
* @param {number} [options.socketTimeoutMS=0] How long the MongoDB driver will wait before killing a socket due to inactivity _after initial connection_. A socket may be inactive because of either no activity or a long-running operation. `socketTimeoutMS` defaults to 0, which means Node.js will not time out the socket due to inactivity. This option is passed to [Node.js `socket#setTimeout()` function](https://nodejs.org/api/net.html#net_socket_settimeout_timeout_callback) after the MongoDB driver successfully completes.
|
|
453
451
|
* @param {number} [options.family=0] Passed transparently to [Node.js' `dns.lookup()`](https://nodejs.org/api/dns.html#dns_dns_lookup_hostname_options_callback) function. May be either `0`, `4`, or `6`. `4` means use IPv4 only, `6` means use IPv6 only, `0` means try both.
|
|
454
452
|
* @param {boolean} [options.autoCreate=false] Set to `true` to make Mongoose automatically call `createCollection()` on every model created on this connection.
|
|
@@ -501,7 +499,7 @@ Mongoose.prototype.disconnect = async function disconnect() {
|
|
|
501
499
|
* Sessions are scoped to a connection, so calling `mongoose.startSession()`
|
|
502
500
|
* starts a session on the [default mongoose connection](https://mongoosejs.com/docs/api/mongoose.html#Mongoose.prototype.connection).
|
|
503
501
|
*
|
|
504
|
-
* @param {object} [options] see the [mongodb driver options](https://mongodb.github.io/node-mongodb-native/
|
|
502
|
+
* @param {object} [options] see the [mongodb driver options](https://mongodb.github.io/node-mongodb-native/7.0/classes/MongoClient.html#startSession)
|
|
505
503
|
* @param {boolean} [options.causalConsistency=true] set to false to disable causal consistency
|
|
506
504
|
* @return {Promise<ClientSession>} promise that resolves to a MongoDB driver `ClientSession`
|
|
507
505
|
* @api public
|
|
@@ -1026,7 +1024,7 @@ Mongoose.prototype.VirtualType = VirtualType;
|
|
|
1026
1024
|
* - [Array](https://mongoosejs.com/docs/schematypes.html#arrays)
|
|
1027
1025
|
* - [Buffer](https://mongoosejs.com/docs/schematypes.html#buffers)
|
|
1028
1026
|
* - [Embedded](https://mongoosejs.com/docs/schematypes.html#schemas)
|
|
1029
|
-
* - [DocumentArray](https://mongoosejs.com/docs/api/
|
|
1027
|
+
* - [DocumentArray](https://mongoosejs.com/docs/api/documentarray.html)
|
|
1030
1028
|
* - [Decimal128](https://mongoosejs.com/docs/api/mongoose.html#Mongoose.prototype.Decimal128)
|
|
1031
1029
|
* - [ObjectId](https://mongoosejs.com/docs/schematypes.html#objectids)
|
|
1032
1030
|
* - [Map](https://mongoosejs.com/docs/schematypes.html#maps)
|
|
@@ -61,6 +61,29 @@ Object.defineProperty(SchemaDocumentArrayOptions.prototype, 'excludeIndexes', op
|
|
|
61
61
|
|
|
62
62
|
Object.defineProperty(SchemaDocumentArrayOptions.prototype, '_id', opts);
|
|
63
63
|
|
|
64
|
+
/**
|
|
65
|
+
* Defines embedded discriminators for this document array path.
|
|
66
|
+
*
|
|
67
|
+
* #### Example:
|
|
68
|
+
*
|
|
69
|
+
* const eventSchema = Schema({ message: String }, { discriminatorKey: 'kind' });
|
|
70
|
+
* const clickedSchema = Schema({ element: String });
|
|
71
|
+
* const parentSchema = Schema({
|
|
72
|
+
* events: [{
|
|
73
|
+
* type: eventSchema,
|
|
74
|
+
* discriminators: { Clicked: clickedSchema }
|
|
75
|
+
* }]
|
|
76
|
+
* });
|
|
77
|
+
*
|
|
78
|
+
* @api public
|
|
79
|
+
* @property discriminators
|
|
80
|
+
* @memberOf SchemaDocumentArrayOptions
|
|
81
|
+
* @type {Object}
|
|
82
|
+
* @instance
|
|
83
|
+
*/
|
|
84
|
+
|
|
85
|
+
Object.defineProperty(SchemaDocumentArrayOptions.prototype, 'discriminators', opts);
|
|
86
|
+
|
|
64
87
|
/*!
|
|
65
88
|
* ignore
|
|
66
89
|
*/
|
|
@@ -63,4 +63,27 @@ Object.defineProperty(SchemaSubdocumentOptions.prototype, '_id', opts);
|
|
|
63
63
|
|
|
64
64
|
Object.defineProperty(SchemaSubdocumentOptions.prototype, 'minimize', opts);
|
|
65
65
|
|
|
66
|
+
/**
|
|
67
|
+
* Defines embedded discriminators for this single nested path.
|
|
68
|
+
*
|
|
69
|
+
* #### Example:
|
|
70
|
+
*
|
|
71
|
+
* const eventSchema = Schema({ message: String }, { discriminatorKey: 'kind' });
|
|
72
|
+
* const clickedSchema = Schema({ element: String });
|
|
73
|
+
* const parentSchema = Schema({
|
|
74
|
+
* event: {
|
|
75
|
+
* type: eventSchema,
|
|
76
|
+
* discriminators: { Clicked: clickedSchema }
|
|
77
|
+
* }
|
|
78
|
+
* });
|
|
79
|
+
*
|
|
80
|
+
* @api public
|
|
81
|
+
* @property discriminators
|
|
82
|
+
* @memberOf SchemaSubdocumentOptions
|
|
83
|
+
* @type {Object}
|
|
84
|
+
* @instance
|
|
85
|
+
*/
|
|
86
|
+
|
|
87
|
+
Object.defineProperty(SchemaSubdocumentOptions.prototype, 'discriminators', opts);
|
|
88
|
+
|
|
66
89
|
module.exports = SchemaSubdocumentOptions;
|
package/lib/plugins/index.js
CHANGED