mongoose 6.1.1 → 6.1.2
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/index.d.ts +52 -40
- package/lib/aggregate.js +3 -38
- package/lib/drivers/node-mongodb-native/connection.js +1 -0
- package/lib/helpers/aggregate/prepareDiscriminatorPipeline.js +37 -0
- package/lib/helpers/populate/getModelsMapForPopulate.js +11 -2
- package/lib/model.js +4 -1
- package/lib/query.js +25 -12
- package/package.json +2 -2
package/index.d.ts
CHANGED
|
@@ -68,6 +68,15 @@ declare module 'mongoose' {
|
|
|
68
68
|
export function connect(uri: string, callback: CallbackWithoutResult): void;
|
|
69
69
|
export function connect(uri: string, options?: ConnectOptions): Promise<Mongoose>;
|
|
70
70
|
|
|
71
|
+
/**
|
|
72
|
+
* Makes the indexes in MongoDB match the indexes defined in every model's
|
|
73
|
+
* schema. This function will drop any indexes that are not defined in
|
|
74
|
+
* the model's schema except the `_id` index, and build any indexes that
|
|
75
|
+
* are in your schema but not in MongoDB.
|
|
76
|
+
*/
|
|
77
|
+
export function syncIndexes(options?: Record<string, unknown>): Promise<Array<string>>;
|
|
78
|
+
export function syncIndexes(options: Record<string, unknown> | null, callback: Callback<Array<string>>): void;
|
|
79
|
+
|
|
71
80
|
/** The Mongoose module's default connection. Equivalent to `mongoose.connections[0]`, see [`connections`](#mongoose_Mongoose-connections). */
|
|
72
81
|
export const connection: Connection;
|
|
73
82
|
|
|
@@ -420,6 +429,15 @@ declare module 'mongoose' {
|
|
|
420
429
|
startSession(options?: mongodb.ClientSessionOptions): Promise<mongodb.ClientSession>;
|
|
421
430
|
startSession(options: mongodb.ClientSessionOptions, cb: Callback<mongodb.ClientSession>): void;
|
|
422
431
|
|
|
432
|
+
/**
|
|
433
|
+
* Makes the indexes in MongoDB match the indexes defined in every model's
|
|
434
|
+
* schema. This function will drop any indexes that are not defined in
|
|
435
|
+
* the model's schema except the `_id` index, and build any indexes that
|
|
436
|
+
* are in your schema but not in MongoDB.
|
|
437
|
+
*/
|
|
438
|
+
syncIndexes(options?: Record<string, unknown>): Promise<Array<string>>;
|
|
439
|
+
syncIndexes(options: Record<string, unknown> | null, callback: Callback<Array<string>>): void;
|
|
440
|
+
|
|
423
441
|
/**
|
|
424
442
|
* _Requires MongoDB >= 3.6.0._ Executes the wrapped async function
|
|
425
443
|
* in a transaction. Mongoose will commit the transaction if the
|
|
@@ -785,7 +803,7 @@ declare module 'mongoose' {
|
|
|
785
803
|
|
|
786
804
|
/** Creates a `countDocuments` query: counts the number of documents that match `filter`. */
|
|
787
805
|
countDocuments(callback?: Callback<number>): QueryWithHelpers<number, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
|
|
788
|
-
countDocuments(filter: FilterQuery<T>, callback?: Callback<number>): QueryWithHelpers<number, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
|
|
806
|
+
countDocuments(filter: FilterQuery<T>, options?: QueryOptions, callback?: Callback<number>): QueryWithHelpers<number, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
|
|
789
807
|
|
|
790
808
|
/** Creates a new document or documents */
|
|
791
809
|
create(docs: (AnyKeys<T> | AnyObject)[], options?: SaveOptions): Promise<HydratedDocument<T, TMethods, TVirtuals>[]>;
|
|
@@ -1219,7 +1237,7 @@ declare module 'mongoose' {
|
|
|
1219
1237
|
/** remove empty objects (defaults to true) */
|
|
1220
1238
|
minimize?: boolean;
|
|
1221
1239
|
/** if set, mongoose will call this function to allow you to transform the returned object */
|
|
1222
|
-
transform?: (doc: any, ret: any, options: any) => any;
|
|
1240
|
+
transform?: boolean | ((doc: any, ret: any, options: any) => any);
|
|
1223
1241
|
/** if true, replace any conventionally populated paths with the original id in the output. Has no affect on virtual populated paths. */
|
|
1224
1242
|
depopulate?: boolean;
|
|
1225
1243
|
/** if false, exclude the version key (`__v` by default) from the output */
|
|
@@ -1380,14 +1398,19 @@ declare module 'mongoose' {
|
|
|
1380
1398
|
virtualpath(name: string): VirtualType | null;
|
|
1381
1399
|
}
|
|
1382
1400
|
|
|
1401
|
+
type NumberSchemaDefinition = typeof Number | 'number' | 'Number' | typeof Schema.Types.Number;
|
|
1402
|
+
type StringSchemaDefinition = typeof String | 'string' | 'String' | typeof Schema.Types.String;
|
|
1403
|
+
type BooleanSchemaDefinition = typeof Boolean | 'boolean' | 'Boolean' | typeof Schema.Types.Boolean;
|
|
1404
|
+
type DateSchemaDefinition = typeof NativeDate | 'date' | 'Date' | typeof Schema.Types.Date;
|
|
1405
|
+
|
|
1383
1406
|
type SchemaDefinitionWithBuiltInClass<T> = T extends number
|
|
1384
|
-
?
|
|
1407
|
+
? NumberSchemaDefinition
|
|
1385
1408
|
: T extends string
|
|
1386
|
-
?
|
|
1409
|
+
? StringSchemaDefinition
|
|
1387
1410
|
: T extends boolean
|
|
1388
|
-
?
|
|
1411
|
+
? BooleanSchemaDefinition
|
|
1389
1412
|
: T extends NativeDate
|
|
1390
|
-
?
|
|
1413
|
+
? DateSchemaDefinition
|
|
1391
1414
|
: (Function | string);
|
|
1392
1415
|
|
|
1393
1416
|
type SchemaDefinitionProperty<T = undefined> = SchemaDefinitionWithBuiltInClass<T> |
|
|
@@ -1568,16 +1591,18 @@ declare module 'mongoose' {
|
|
|
1568
1591
|
|
|
1569
1592
|
export class SchemaTypeOptions<T> {
|
|
1570
1593
|
type?:
|
|
1571
|
-
T extends string
|
|
1572
|
-
T extends
|
|
1594
|
+
T extends string ? StringSchemaDefinition :
|
|
1595
|
+
T extends number ? NumberSchemaDefinition :
|
|
1596
|
+
T extends boolean ? BooleanSchemaDefinition :
|
|
1597
|
+
T extends NativeDate ? DateSchemaDefinition :
|
|
1573
1598
|
T extends Map<any, any> ? SchemaDefinition<typeof Map> :
|
|
1574
1599
|
T extends Buffer ? SchemaDefinition<typeof Buffer> :
|
|
1575
1600
|
T extends object[] ? (AnyArray<Schema<any, any, any>> | AnyArray<SchemaDefinition<Unpacked<T>>> | AnyArray<SchemaTypeOptions<Unpacked<T>>>) :
|
|
1576
|
-
T extends string[] ? AnyArray<
|
|
1577
|
-
T extends number[] ? AnyArray<
|
|
1578
|
-
T extends boolean[] ? AnyArray<
|
|
1579
|
-
T extends Function[] ? AnyArray<
|
|
1580
|
-
T | typeof SchemaType | Schema<any, any, any> | SchemaDefinition<T>;
|
|
1601
|
+
T extends string[] ? AnyArray<StringSchemaDefinition> | AnyArray<SchemaTypeOptions<string>> :
|
|
1602
|
+
T extends number[] ? AnyArray<NumberSchemaDefinition> | AnyArray<SchemaTypeOptions<number>> :
|
|
1603
|
+
T extends boolean[] ? AnyArray<BooleanSchemaDefinition> | AnyArray<SchemaTypeOptions<boolean>> :
|
|
1604
|
+
T extends Function[] ? AnyArray<Function | string> | AnyArray<SchemaTypeOptions<Unpacked<T>>> :
|
|
1605
|
+
T | typeof SchemaType | Schema<any, any, any> | SchemaDefinition<T> | Function | AnyArray<Function>;
|
|
1581
1606
|
|
|
1582
1607
|
/** Defines a virtual with the given name that gets/sets this path. */
|
|
1583
1608
|
alias?: string;
|
|
@@ -2157,7 +2182,7 @@ declare module 'mongoose' {
|
|
|
2157
2182
|
|
|
2158
2183
|
/** Specifies this query as a `countDocuments` query. */
|
|
2159
2184
|
countDocuments(callback?: Callback<number>): QueryWithHelpers<number, DocType, THelpers, RawDocType>;
|
|
2160
|
-
countDocuments(criteria: FilterQuery<DocType>, callback?: Callback<number>): QueryWithHelpers<number, DocType, THelpers, RawDocType>;
|
|
2185
|
+
countDocuments(criteria: FilterQuery<DocType>, options?: QueryOptions, callback?: Callback<number>): QueryWithHelpers<number, DocType, THelpers, RawDocType>;
|
|
2161
2186
|
|
|
2162
2187
|
/**
|
|
2163
2188
|
* Returns a wrapper around a [mongodb driver cursor](http://mongodb.github.io/node-mongodb-native/2.1/api/Cursor.html).
|
|
@@ -2572,16 +2597,7 @@ declare module 'mongoose' {
|
|
|
2572
2597
|
[key: string]: any;
|
|
2573
2598
|
};
|
|
2574
2599
|
|
|
2575
|
-
type
|
|
2576
|
-
[key in keyof TSchema]?: TSchema[key];
|
|
2577
|
-
};
|
|
2578
|
-
|
|
2579
|
-
type MatchKeysAndValues<TSchema> = ReadonlyPartial<TSchema> & AnyObject;
|
|
2580
|
-
|
|
2581
|
-
type ApplyBasicQueryCasting<T> = T extends mongodb.ObjectId ? T | string | (T | string)[] : // Allow strings for ObjectIds
|
|
2582
|
-
T extends string ? T | RegExp | T[] : // Allow RegExps for strings
|
|
2583
|
-
T extends (infer U)[] ? T | U : // Allow single array elements for arrays
|
|
2584
|
-
T | T[];
|
|
2600
|
+
type ApplyBasicQueryCasting<T> = T | T[] | any;
|
|
2585
2601
|
type Condition<T> = ApplyBasicQueryCasting<T> | QuerySelector<ApplyBasicQueryCasting<T>>;
|
|
2586
2602
|
|
|
2587
2603
|
type _FilterQuery<T> = {
|
|
@@ -2604,30 +2620,26 @@ declare module 'mongoose' {
|
|
|
2604
2620
|
$sort?: SortValues | Record<string, SortValues>;
|
|
2605
2621
|
};
|
|
2606
2622
|
|
|
2607
|
-
type OnlyFieldsOfType<TSchema, FieldType = any, AssignableType = FieldType> = {
|
|
2608
|
-
[key in keyof TSchema]?: [Extract<TSchema[key], FieldType>] extends [never] ? never : AssignableType;
|
|
2609
|
-
};
|
|
2610
|
-
|
|
2611
2623
|
type NumericTypes = number | Decimal128 | mongodb.Double | mongodb.Int32 | mongodb.Long;
|
|
2612
2624
|
|
|
2613
2625
|
type _UpdateQuery<TSchema> = {
|
|
2614
2626
|
/** @see https://docs.mongodb.com/manual/reference/operator/update-field/ */
|
|
2615
|
-
$currentDate?:
|
|
2616
|
-
$inc?:
|
|
2627
|
+
$currentDate?: AnyKeys<TSchema> & AnyObject;
|
|
2628
|
+
$inc?: AnyKeys<TSchema> & AnyObject;
|
|
2617
2629
|
$min?: AnyKeys<TSchema> & AnyObject;
|
|
2618
2630
|
$max?: AnyKeys<TSchema> & AnyObject;
|
|
2619
|
-
$mul?:
|
|
2631
|
+
$mul?: AnyKeys<TSchema> & AnyObject;
|
|
2620
2632
|
$rename?: { [key: string]: string };
|
|
2621
2633
|
$set?: AnyKeys<TSchema> & AnyObject;
|
|
2622
2634
|
$setOnInsert?: AnyKeys<TSchema> & AnyObject;
|
|
2623
2635
|
$unset?: AnyKeys<TSchema> & AnyObject;
|
|
2624
2636
|
|
|
2625
2637
|
/** @see https://docs.mongodb.com/manual/reference/operator/update-array/ */
|
|
2626
|
-
$addToSet?:
|
|
2627
|
-
$pop?:
|
|
2628
|
-
$pull?:
|
|
2629
|
-
$push?:
|
|
2630
|
-
$pullAll?:
|
|
2638
|
+
$addToSet?: AnyKeys<TSchema> & AnyObject;
|
|
2639
|
+
$pop?: AnyKeys<TSchema> & AnyObject;
|
|
2640
|
+
$pull?: AnyKeys<TSchema> & AnyObject;
|
|
2641
|
+
$push?: AnyKeys<TSchema> & AnyObject;
|
|
2642
|
+
$pullAll?: AnyKeys<TSchema> & AnyObject;
|
|
2631
2643
|
|
|
2632
2644
|
/** @see https://docs.mongodb.com/manual/reference/operator/update-bitwise/ */
|
|
2633
2645
|
$bit?: {
|
|
@@ -2647,10 +2659,10 @@ declare module 'mongoose' {
|
|
|
2647
2659
|
[Extract<T, mongodb.ObjectId>] extends [never] ? T :
|
|
2648
2660
|
T | string;
|
|
2649
2661
|
type _UpdateQueryDef<T> = {
|
|
2650
|
-
[K in keyof T]
|
|
2662
|
+
[K in keyof T]?: __UpdateDefProperty<T[K]>;
|
|
2651
2663
|
};
|
|
2652
2664
|
|
|
2653
|
-
export type UpdateQuery<T> =
|
|
2665
|
+
export type UpdateQuery<T> = _UpdateQuery<_UpdateQueryDef<T>> & AnyObject;
|
|
2654
2666
|
|
|
2655
2667
|
export type DocumentDefinition<T> = {
|
|
2656
2668
|
[K in keyof Omit<T, Exclude<keyof Document, '_id' | 'id' | '__v'>>]:
|
|
@@ -2846,7 +2858,7 @@ declare module 'mongoose' {
|
|
|
2846
2858
|
redact(expression: any, thenExpr: string | any, elseExpr: string | any): this;
|
|
2847
2859
|
|
|
2848
2860
|
/** Appends a new $replaceRoot operator to this aggregate pipeline. */
|
|
2849
|
-
replaceRoot(newRoot: PipelineStage.ReplaceRoot['$replaceRoot'] | string): this;
|
|
2861
|
+
replaceRoot(newRoot: PipelineStage.ReplaceRoot['$replaceRoot']['newRoot'] | string): this;
|
|
2850
2862
|
|
|
2851
2863
|
/**
|
|
2852
2864
|
* Helper for [Atlas Text Search](https://docs.atlas.mongodb.com/reference/atlas-search/tutorial/)'s
|
|
@@ -3177,7 +3189,7 @@ declare module 'mongoose' {
|
|
|
3177
3189
|
/** [`$unionWith` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/unionWith/) */
|
|
3178
3190
|
$unionWith:
|
|
3179
3191
|
| string
|
|
3180
|
-
| { coll: string; pipeline?: Exclude<PipelineStage, PipelineStage.Out | PipelineStage.Merge> }
|
|
3192
|
+
| { coll: string; pipeline?: Exclude<PipelineStage, PipelineStage.Out | PipelineStage.Merge>[] }
|
|
3181
3193
|
}
|
|
3182
3194
|
|
|
3183
3195
|
export interface Unset {
|
package/lib/aggregate.js
CHANGED
|
@@ -8,6 +8,7 @@ const AggregationCursor = require('./cursor/AggregationCursor');
|
|
|
8
8
|
const Query = require('./query');
|
|
9
9
|
const applyGlobalMaxTimeMS = require('./helpers/query/applyGlobalMaxTimeMS');
|
|
10
10
|
const getConstructorName = require('./helpers/getConstructorName');
|
|
11
|
+
const prepareDiscriminatorPipeline = require('./helpers/aggregate/prepareDiscriminatorPipeline');
|
|
11
12
|
const promiseOrCallback = require('./helpers/promiseOrCallback');
|
|
12
13
|
const stringifyFunctionOperators = require('./helpers/aggregate/stringifyFunctionOperators');
|
|
13
14
|
const util = require('util');
|
|
@@ -722,7 +723,7 @@ Aggregate.prototype.explain = function(callback) {
|
|
|
722
723
|
return cb(err);
|
|
723
724
|
}
|
|
724
725
|
|
|
725
|
-
prepareDiscriminatorPipeline(this);
|
|
726
|
+
prepareDiscriminatorPipeline(this._pipeline, this._model.schema);
|
|
726
727
|
|
|
727
728
|
model.hooks.execPre('aggregate', this, error => {
|
|
728
729
|
if (error) {
|
|
@@ -978,7 +979,7 @@ Aggregate.prototype.exec = function(callback) {
|
|
|
978
979
|
}
|
|
979
980
|
|
|
980
981
|
return promiseOrCallback(callback, cb => {
|
|
981
|
-
prepareDiscriminatorPipeline(this);
|
|
982
|
+
prepareDiscriminatorPipeline(this._pipeline, this._model.schema);
|
|
982
983
|
stringifyFunctionOperators(this._pipeline);
|
|
983
984
|
|
|
984
985
|
model.hooks.execPre('aggregate', this, error => {
|
|
@@ -1109,42 +1110,6 @@ function isOperator(obj) {
|
|
|
1109
1110
|
|
|
1110
1111
|
Aggregate._prepareDiscriminatorPipeline = prepareDiscriminatorPipeline;
|
|
1111
1112
|
|
|
1112
|
-
function prepareDiscriminatorPipeline(aggregate) {
|
|
1113
|
-
const schema = aggregate._model.schema;
|
|
1114
|
-
const discriminatorMapping = schema && schema.discriminatorMapping;
|
|
1115
|
-
|
|
1116
|
-
if (discriminatorMapping && !discriminatorMapping.isRoot) {
|
|
1117
|
-
const originalPipeline = aggregate._pipeline;
|
|
1118
|
-
const discriminatorKey = discriminatorMapping.key;
|
|
1119
|
-
const discriminatorValue = discriminatorMapping.value;
|
|
1120
|
-
|
|
1121
|
-
// If the first pipeline stage is a match and it doesn't specify a `__t`
|
|
1122
|
-
// key, add the discriminator key to it. This allows for potential
|
|
1123
|
-
// aggregation query optimizations not to be disturbed by this feature.
|
|
1124
|
-
if (originalPipeline[0] != null && originalPipeline[0].$match && !originalPipeline[0].$match[discriminatorKey]) {
|
|
1125
|
-
originalPipeline[0].$match[discriminatorKey] = discriminatorValue;
|
|
1126
|
-
// `originalPipeline` is a ref, so there's no need for
|
|
1127
|
-
// aggregate._pipeline = originalPipeline
|
|
1128
|
-
} else if (originalPipeline[0] != null && originalPipeline[0].$geoNear) {
|
|
1129
|
-
originalPipeline[0].$geoNear.query =
|
|
1130
|
-
originalPipeline[0].$geoNear.query || {};
|
|
1131
|
-
originalPipeline[0].$geoNear.query[discriminatorKey] = discriminatorValue;
|
|
1132
|
-
} else if (originalPipeline[0] != null && originalPipeline[0].$search) {
|
|
1133
|
-
if (originalPipeline[1] && originalPipeline[1].$match != null) {
|
|
1134
|
-
originalPipeline[1].$match[discriminatorKey] = originalPipeline[1].$match[discriminatorKey] || discriminatorValue;
|
|
1135
|
-
} else {
|
|
1136
|
-
const match = {};
|
|
1137
|
-
match[discriminatorKey] = discriminatorValue;
|
|
1138
|
-
originalPipeline.splice(1, 0, { $match: match });
|
|
1139
|
-
}
|
|
1140
|
-
} else {
|
|
1141
|
-
const match = {};
|
|
1142
|
-
match[discriminatorKey] = discriminatorValue;
|
|
1143
|
-
aggregate._pipeline.unshift({ $match: match });
|
|
1144
|
-
}
|
|
1145
|
-
}
|
|
1146
|
-
}
|
|
1147
|
-
|
|
1148
1113
|
/*!
|
|
1149
1114
|
* Exports
|
|
1150
1115
|
*/
|
|
@@ -71,6 +71,7 @@ NativeConnection.prototype.useDb = function(name, options) {
|
|
|
71
71
|
newConn._closeCalled = this._closeCalled;
|
|
72
72
|
newConn._hasOpened = this._hasOpened;
|
|
73
73
|
newConn._listening = false;
|
|
74
|
+
newConn._parent = this;
|
|
74
75
|
|
|
75
76
|
newConn.host = this.host;
|
|
76
77
|
newConn.port = this.port;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
module.exports = function prepareDiscriminatorPipeline(pipeline, schema, prefix) {
|
|
4
|
+
const discriminatorMapping = schema && schema.discriminatorMapping;
|
|
5
|
+
prefix = prefix || '';
|
|
6
|
+
|
|
7
|
+
if (discriminatorMapping && !discriminatorMapping.isRoot) {
|
|
8
|
+
const originalPipeline = pipeline;
|
|
9
|
+
const filterKey = (prefix.length > 0 ? prefix + '.' : prefix) + discriminatorMapping.key;
|
|
10
|
+
const discriminatorValue = discriminatorMapping.value;
|
|
11
|
+
|
|
12
|
+
// If the first pipeline stage is a match and it doesn't specify a `__t`
|
|
13
|
+
// key, add the discriminator key to it. This allows for potential
|
|
14
|
+
// aggregation query optimizations not to be disturbed by this feature.
|
|
15
|
+
if (originalPipeline[0] != null && originalPipeline[0].$match && !originalPipeline[0].$match[filterKey]) {
|
|
16
|
+
originalPipeline[0].$match[filterKey] = discriminatorValue;
|
|
17
|
+
// `originalPipeline` is a ref, so there's no need for
|
|
18
|
+
// aggregate._pipeline = originalPipeline
|
|
19
|
+
} else if (originalPipeline[0] != null && originalPipeline[0].$geoNear) {
|
|
20
|
+
originalPipeline[0].$geoNear.query =
|
|
21
|
+
originalPipeline[0].$geoNear.query || {};
|
|
22
|
+
originalPipeline[0].$geoNear.query[filterKey] = discriminatorValue;
|
|
23
|
+
} else if (originalPipeline[0] != null && originalPipeline[0].$search) {
|
|
24
|
+
if (originalPipeline[1] && originalPipeline[1].$match != null) {
|
|
25
|
+
originalPipeline[1].$match[filterKey] = originalPipeline[1].$match[filterKey] || discriminatorValue;
|
|
26
|
+
} else {
|
|
27
|
+
const match = {};
|
|
28
|
+
match[filterKey] = discriminatorValue;
|
|
29
|
+
originalPipeline.splice(1, 0, { $match: match });
|
|
30
|
+
}
|
|
31
|
+
} else {
|
|
32
|
+
const match = {};
|
|
33
|
+
match[filterKey] = discriminatorValue;
|
|
34
|
+
originalPipeline.unshift({ $match: match });
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
};
|
|
@@ -252,7 +252,7 @@ module.exports = function getModelsMapForPopulate(model, docs, options) {
|
|
|
252
252
|
modelForCurrentDoc = discriminatorModel;
|
|
253
253
|
} else {
|
|
254
254
|
try {
|
|
255
|
-
modelForCurrentDoc = model.db
|
|
255
|
+
modelForCurrentDoc = _getModelFromConn(model.db, discriminatorValue);
|
|
256
256
|
} catch (error) {
|
|
257
257
|
return error;
|
|
258
258
|
}
|
|
@@ -491,7 +491,7 @@ function addModelNamesToMap(model, map, available, modelNames, options, data, re
|
|
|
491
491
|
Model = modelName;
|
|
492
492
|
} else {
|
|
493
493
|
try {
|
|
494
|
-
Model = connection
|
|
494
|
+
Model = _getModelFromConn(connection, modelName);
|
|
495
495
|
} catch (err) {
|
|
496
496
|
if (ret !== void 0) {
|
|
497
497
|
throw err;
|
|
@@ -558,6 +558,15 @@ function addModelNamesToMap(model, map, available, modelNames, options, data, re
|
|
|
558
558
|
}
|
|
559
559
|
}
|
|
560
560
|
|
|
561
|
+
function _getModelFromConn(conn, modelName) {
|
|
562
|
+
/* If this connection has a parent from `useDb()`, bubble up to parent's models */
|
|
563
|
+
if (conn.models[modelName] == null && conn._parent != null) {
|
|
564
|
+
return _getModelFromConn(conn._parent, modelName);
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
return conn.model(modelName);
|
|
568
|
+
}
|
|
569
|
+
|
|
561
570
|
/*!
|
|
562
571
|
* ignore
|
|
563
572
|
*/
|
package/lib/model.js
CHANGED
|
@@ -46,6 +46,7 @@ const isPathSelectedInclusive = require('./helpers/projection/isPathSelectedIncl
|
|
|
46
46
|
const leanPopulateMap = require('./helpers/populate/leanPopulateMap');
|
|
47
47
|
const modifiedPaths = require('./helpers/update/modifiedPaths');
|
|
48
48
|
const parallelLimit = require('./helpers/parallelLimit');
|
|
49
|
+
const prepareDiscriminatorPipeline = require('./helpers/aggregate/prepareDiscriminatorPipeline');
|
|
49
50
|
const removeDeselectedForeignField = require('./helpers/populate/removeDeselectedForeignField');
|
|
50
51
|
const util = require('util');
|
|
51
52
|
const utils = require('./utils');
|
|
@@ -2263,7 +2264,7 @@ Model.findOne = function findOne(conditions, projection, options, callback) {
|
|
|
2263
2264
|
*
|
|
2264
2265
|
* ####Example:
|
|
2265
2266
|
*
|
|
2266
|
-
* const numAdventures = Adventure.estimatedDocumentCount();
|
|
2267
|
+
* const numAdventures = await Adventure.estimatedDocumentCount();
|
|
2267
2268
|
*
|
|
2268
2269
|
* @param {Object} [options]
|
|
2269
2270
|
* @param {Function} [callback]
|
|
@@ -3197,6 +3198,8 @@ Model.watch = function(pipeline, options) {
|
|
|
3197
3198
|
_checkContext(this, 'watch');
|
|
3198
3199
|
|
|
3199
3200
|
const changeStreamThunk = cb => {
|
|
3201
|
+
pipeline = pipeline || [];
|
|
3202
|
+
prepareDiscriminatorPipeline(pipeline, this.schema, 'fullDocument');
|
|
3200
3203
|
if (this.$__collection.buffer) {
|
|
3201
3204
|
this.$__collection.addQueue(() => {
|
|
3202
3205
|
if (this.closed) {
|
package/lib/query.js
CHANGED
|
@@ -2684,18 +2684,24 @@ Query.prototype.estimatedDocumentCount = function(options, callback) {
|
|
|
2684
2684
|
* - `$nearSphere`: [`$geoWithin`](https://docs.mongodb.com/manual/reference/operator/query/geoWithin/) with [`$centerSphere`](https://docs.mongodb.com/manual/reference/operator/query/centerSphere/#op._S_centerSphere)
|
|
2685
2685
|
*
|
|
2686
2686
|
* @param {Object} [filter] mongodb selector
|
|
2687
|
+
* @param {Object} [options]
|
|
2687
2688
|
* @param {Function} [callback] optional params are (error, count)
|
|
2688
2689
|
* @return {Query} this
|
|
2689
2690
|
* @see countDocuments http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#countDocuments
|
|
2690
2691
|
* @api public
|
|
2691
2692
|
*/
|
|
2692
2693
|
|
|
2693
|
-
Query.prototype.countDocuments = function(conditions, callback) {
|
|
2694
|
+
Query.prototype.countDocuments = function(conditions, options, callback) {
|
|
2694
2695
|
this.op = 'countDocuments';
|
|
2695
2696
|
this._validateOp();
|
|
2696
2697
|
if (typeof conditions === 'function') {
|
|
2697
2698
|
callback = conditions;
|
|
2698
2699
|
conditions = undefined;
|
|
2700
|
+
options = undefined;
|
|
2701
|
+
}
|
|
2702
|
+
if (typeof options === 'function') {
|
|
2703
|
+
callback = options;
|
|
2704
|
+
options = undefined;
|
|
2699
2705
|
}
|
|
2700
2706
|
|
|
2701
2707
|
conditions = utils.toObject(conditions);
|
|
@@ -2704,6 +2710,10 @@ Query.prototype.countDocuments = function(conditions, callback) {
|
|
|
2704
2710
|
this.merge(conditions);
|
|
2705
2711
|
}
|
|
2706
2712
|
|
|
2713
|
+
if (typeof options === 'object' && options != null) {
|
|
2714
|
+
this.setOptions(options);
|
|
2715
|
+
}
|
|
2716
|
+
|
|
2707
2717
|
if (!callback) {
|
|
2708
2718
|
return this;
|
|
2709
2719
|
}
|
|
@@ -4444,7 +4454,7 @@ function _update(query, op, filter, doc, options, callback) {
|
|
|
4444
4454
|
* Runs a function `fn` and treats the return value of `fn` as the new value
|
|
4445
4455
|
* for the query to resolve to.
|
|
4446
4456
|
*
|
|
4447
|
-
* Any functions you pass to `
|
|
4457
|
+
* Any functions you pass to `transform()` will run **after** any post hooks.
|
|
4448
4458
|
*
|
|
4449
4459
|
* ####Example:
|
|
4450
4460
|
*
|
|
@@ -4456,7 +4466,7 @@ function _update(query, op, filter, doc, options, callback) {
|
|
|
4456
4466
|
* Object.assign(res, { loadedAt: new Date() });
|
|
4457
4467
|
* });
|
|
4458
4468
|
*
|
|
4459
|
-
* @method
|
|
4469
|
+
* @method transform
|
|
4460
4470
|
* @memberOf Query
|
|
4461
4471
|
* @instance
|
|
4462
4472
|
* @param {Function} fn function to run to transform the query result
|
|
@@ -5180,9 +5190,12 @@ Query.prototype.maxscan = Query.base.maxScan;
|
|
|
5180
5190
|
*
|
|
5181
5191
|
* ####Example
|
|
5182
5192
|
*
|
|
5183
|
-
* query.tailable() // true
|
|
5184
|
-
* query.tailable(true)
|
|
5185
|
-
* query.tailable(false)
|
|
5193
|
+
* query.tailable(); // true
|
|
5194
|
+
* query.tailable(true);
|
|
5195
|
+
* query.tailable(false);
|
|
5196
|
+
*
|
|
5197
|
+
* // Set both `tailable` and `awaitData` options
|
|
5198
|
+
* query.tailable({ awaitData: true });
|
|
5186
5199
|
*
|
|
5187
5200
|
* ####Note
|
|
5188
5201
|
*
|
|
@@ -5190,15 +5203,15 @@ Query.prototype.maxscan = Query.base.maxScan;
|
|
|
5190
5203
|
*
|
|
5191
5204
|
* @param {Boolean} bool defaults to true
|
|
5192
5205
|
* @param {Object} [opts] options to set
|
|
5193
|
-
* @param {
|
|
5194
|
-
* @param {Number} [opts.
|
|
5206
|
+
* @param {Boolean} [opts.awaitData] false by default. Set to true to keep the cursor open even if there's no data.
|
|
5207
|
+
* @param {Number} [opts.maxAwaitTimeMS] the maximum amount of time for the server to wait on new documents to satisfy a tailable cursor query. Requires `tailable` and `awaitData` to be true
|
|
5195
5208
|
* @see tailable http://docs.mongodb.org/manual/tutorial/create-tailable-cursor/
|
|
5196
5209
|
* @api public
|
|
5197
5210
|
*/
|
|
5198
5211
|
|
|
5199
5212
|
Query.prototype.tailable = function(val, opts) {
|
|
5200
|
-
// we need to support the tailable({
|
|
5201
|
-
// tailable(true, {
|
|
5213
|
+
// we need to support the tailable({ awaitData : true }) as well as the
|
|
5214
|
+
// tailable(true, {awaitData :true}) syntax that mquery does not support
|
|
5202
5215
|
if (val != null && typeof val.constructor === 'function' && val.constructor.name === 'Object') {
|
|
5203
5216
|
opts = val;
|
|
5204
5217
|
val = true;
|
|
@@ -5210,9 +5223,9 @@ Query.prototype.tailable = function(val, opts) {
|
|
|
5210
5223
|
|
|
5211
5224
|
if (opts && typeof opts === 'object') {
|
|
5212
5225
|
for (const key of Object.keys(opts)) {
|
|
5213
|
-
if (key === 'awaitdata') {
|
|
5226
|
+
if (key === 'awaitData' || key === 'awaitdata') { // backwards compat, see gh-10875
|
|
5214
5227
|
// For backwards compatibility
|
|
5215
|
-
this.options[
|
|
5228
|
+
this.options['awaitData'] = !!opts[key];
|
|
5216
5229
|
} else {
|
|
5217
5230
|
this.options[key] = opts[key];
|
|
5218
5231
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mongoose",
|
|
3
3
|
"description": "Mongoose MongoDB ODM",
|
|
4
|
-
"version": "6.1.
|
|
4
|
+
"version": "6.1.2",
|
|
5
5
|
"author": "Guillermo Rauch <guillermo@learnboost.com>",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"mongodb",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"dependencies": {
|
|
22
22
|
"bson": "^4.2.2",
|
|
23
23
|
"kareem": "2.3.2",
|
|
24
|
-
"mongodb": "4.2.
|
|
24
|
+
"mongodb": "4.2.2",
|
|
25
25
|
"mpath": "0.8.4",
|
|
26
26
|
"mquery": "4.0.0",
|
|
27
27
|
"ms": "2.1.2",
|