-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11368 from Uzlopak/extract-types-PipelineState
Extract PipelineStage typings from index.d.ts
- 8.9.3
- 8.9.2
- 8.9.1
- 8.9.0
- 8.8.4
- 8.8.3
- 8.8.2
- 8.8.1
- 8.8.0
- 8.7.3
- 8.7.2
- 8.7.1
- 8.7.0
- 8.6.4
- 8.6.3
- 8.6.2
- 8.6.1
- 8.6.0
- 8.5.5
- 8.5.4
- 8.5.3
- 8.5.2
- 8.5.1
- 8.5.0
- 8.4.5
- 8.4.4
- 8.4.3
- 8.4.2
- 8.4.1
- 8.4.0
- 8.3.5
- 8.3.4
- 8.3.3
- 8.3.2
- 8.3.1
- 8.3.0
- 8.2.4
- 8.2.3
- 8.2.2
- 8.2.1
- 8.2.0
- 8.1.3
- 8.1.2
- 8.1.1
- 8.1.0
- 8.0.4
- 8.0.3
- 8.0.2
- 8.0.1
- 8.0.0
- 8.0.0-rc0
- 7.8.3
- 7.8.2
- 7.8.1
- 7.8.0
- 7.7.0
- 7.6.13
- 7.6.12
- 7.6.11
- 7.6.10
- 7.6.9
- 7.6.8
- 7.6.7
- 7.6.6
- 7.6.5
- 7.6.4
- 7.6.3
- 7.6.2
- 7.6.1
- 7.6.0
- 7.5.4
- 7.5.3
- 7.5.2
- 7.5.1
- 7.5.0
- 7.4.5
- 7.4.4
- 7.4.3
- 7.4.2
- 7.4.1
- 7.4.0
- 7.3.4
- 7.3.3
- 7.3.2
- 7.3.1
- 7.3.0
- 7.2.4
- 7.2.3
- 7.2.2
- 7.2.1
- 7.2.0
- 7.1.2
- 7.1.1
- 7.1.0
- 7.0.5
- 7.0.4
- 7.0.3
- 7.0.2
- 7.0.1
- 7.0.0
- 7.0.0-rc0
- 6.13.5
- 6.13.4
- 6.13.3
- 6.13.2
- 6.13.1
- 6.13.0
- 6.12.9
- 6.12.8
- 6.12.7
- 6.12.6
- 6.12.5
- 6.12.4
- 6.12.2
- 6.12.1
- 6.12.0
- 6.11.6
- 6.11.5
- 6.11.4
- 6.11.3
- 6.11.2
- 6.11.1
- 6.11.0
- 6.10.5
- 6.10.4
- 6.10.3
- 6.10.2
- 6.10.1
- 6.10.0
- 6.9.3
- 6.9.2
- 6.9.1
- 6.9.0
- 6.8.5
- 6.8.4
- 6.8.3
- 6.8.2
- 6.8.1
- 6.8.0
- 6.7.5
- 6.7.4
- 6.7.3
- 6.7.2
- 6.7.1
- 6.7.0
- 6.6.7
- 6.6.6
- 6.6.5
- 6.6.4
- 6.6.3
- 6.6.2
- 6.6.1
- 6.6.0
- 6.5.5
- 6.5.4
- 6.5.3
- 6.5.2
- 6.5.1
- 6.5.0
- 6.4.7
- 6.4.6
- 6.4.5
- 6.4.4
- 6.4.3
- 6.4.2
- 6.4.1
- 6.4.0
- 6.3.9
- 6.3.8
- 6.3.7
- 6.3.6
- 6.3.5
- 6.3.4
- 6.3.3
- 6.3.2
- 6.3.1
- 6.3.0
- 6.2.11
- 6.2.10
- 6.2.9
- 6.2.8
- 6.2.7
- 6.2.6
- 6.2.5
- 6.2.4
- 6.2.3
- 6.2.2
Showing
4 changed files
with
409 additions
and
272 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import type { PipelineStage } from 'mongoose'; | ||
import { expectError, expectType } from 'tsd'; | ||
|
||
/** | ||
* $addFields: | ||
* | ||
* @see https://docs.mongodb.com/manual/reference/operator/aggregation/addFields/ | ||
*/ | ||
const addFields1: PipelineStage = { | ||
$addFields: { | ||
totalHomework: { $sum: '$homework' }, | ||
totalQuiz: { $sum: '$quiz' } | ||
} | ||
}; | ||
|
||
const addFields2: PipelineStage = { | ||
$addFields: { | ||
totalScore: | ||
{ $add: ['$totalHomework', '$totalQuiz', '$extraCredit'] } | ||
} | ||
}; | ||
|
||
const addFields3: PipelineStage = { | ||
$addFields: { | ||
'specs.fuel_type': 'unleaded' | ||
} | ||
}; | ||
|
||
const addFields4: PipelineStage = { | ||
$addFields: { cats: 20 } | ||
}; | ||
|
||
const addFields5: PipelineStage = { | ||
$addFields: { | ||
_id: '$item', | ||
item: 'fruit' | ||
} | ||
}; | ||
|
||
const addFields6: PipelineStage = { | ||
$addFields: { homework: { $concatArrays: ['$homework', [7]] } } | ||
}; | ||
|
||
/** | ||
* $bucket | ||
* | ||
* @see https://docs.mongodb.com/manual/reference/operator/aggregation/bucket/ | ||
*/ | ||
|
||
const bucket1: PipelineStage = { | ||
$bucket: { | ||
groupBy: '$year_born', // Field to group by | ||
boundaries: [1840, 1850, 1860, 1870, 1880], // Boundaries for the buckets | ||
default: 'Other', // Bucket id for documents which do not fall into a bucket | ||
output: { // Output for each bucket | ||
count: { $sum: 1 }, | ||
artists: | ||
{ | ||
$push: { | ||
name: { $concat: ['$first_name', ' ', '$last_name'] }, | ||
year_born: '$year_born' | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
const bucket2: PipelineStage = { | ||
$facet: { // Top-level $facet stage | ||
price: [ // Output field 1 | ||
{ | ||
$bucket: { | ||
groupBy: '$price', // Field to group by | ||
boundaries: [0, 200, 400], // Boundaries for the buckets | ||
default: 'Other', // Bucket id for documents which do not fall into a bucket | ||
output: { // Output for each bucket | ||
count: { $sum: 1 }, | ||
artwork: { $push: { title: '$title', price: '$price' } }, | ||
averagePrice: { $avg: '$price' } | ||
} | ||
} | ||
} | ||
], | ||
year: [ // Output field 2 | ||
{ | ||
$bucket: { | ||
groupBy: '$year', // Field to group by | ||
boundaries: [1890, 1910, 1920, 1940], // Boundaries for the buckets | ||
default: 'Unknown', // Bucket id for documents which do not fall into a bucket | ||
output: { // Output for each bucket | ||
count: { $sum: 1 }, | ||
artwork: { $push: { title: '$title', year: '$year' } } | ||
} | ||
} | ||
} | ||
] | ||
} | ||
}; | ||
|
||
/** | ||
* $unionWith | ||
* | ||
* @see https://docs.mongodb.com/manual/reference/operator/aggregation/unionWith/ | ||
*/ | ||
|
||
const unionWith1: PipelineStage = { $unionWith: { coll: 'warehouses', pipeline: [{ $project: { state: 1, _id: 0 } }] } }; | ||
const unionWith2: PipelineStage = { $unionWith: { coll: 'sales2019q2', pipeline: [{ $set: { _id: '2019Q2' } }] } }; | ||
const unionWith3: PipelineStage = { $unionWith: 'sales2019q2' }; | ||
const unionWith4: PipelineStage = { $unionWith: { coll: 'sales2019q2', pipeline: [{ $group: { _id: '$item', total: { $sum: '$quantity' } } }] } }; | ||
|
||
/** | ||
* $unset | ||
* | ||
* @see https://docs.mongodb.com/manual/reference/operator/aggregation/unset/ | ||
*/ | ||
const unset1: PipelineStage = { $unset: '<field.nestedfield>' }; | ||
const unset2: PipelineStage = { $unset: ['isbn', 'copies'] }; | ||
const unset3: PipelineStage = { $unset: ['isbn', 'author.first', 'copies.warehouse'] }; | ||
|
||
|
||
/** | ||
* $unwind | ||
* | ||
* @see https://docs.mongodb.com/manual/reference/operator/aggregation/unwind/ | ||
*/ | ||
|
||
const unwind1: PipelineStage = { $unwind: '$sizes' }; | ||
const unwind2: PipelineStage = { $unwind: { path: '$sizes' } }; | ||
const unwind3: PipelineStage = { $unwind: { path: '$sizes', includeArrayIndex: 'arrayIndex' } }; | ||
const unwind4: PipelineStage = { $unwind: { path: '$sizes', preserveNullAndEmptyArrays: true } }; | ||
const unwind5: PipelineStage = { $unwind: { path: '$sizes', preserveNullAndEmptyArrays: true } }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,272 @@ | ||
declare module 'mongoose' { | ||
/** | ||
* [Stages reference](https://docs.mongodb.com/manual/reference/operator/aggregation-pipeline/#aggregation-pipeline-stages) | ||
*/ | ||
export type PipelineStage = | ||
| PipelineStage.AddFields | ||
| PipelineStage.Bucket | ||
| PipelineStage.BucketAuto | ||
| PipelineStage.CollStats | ||
| PipelineStage.Count | ||
| PipelineStage.Facet | ||
| PipelineStage.GeoNear | ||
| PipelineStage.GraphLookup | ||
| PipelineStage.Group | ||
| PipelineStage.IndexStats | ||
| PipelineStage.Limit | ||
| PipelineStage.ListSessions | ||
| PipelineStage.Lookup | ||
| PipelineStage.Match | ||
| PipelineStage.Merge | ||
| PipelineStage.Out | ||
| PipelineStage.PlanCacheStats | ||
| PipelineStage.Project | ||
| PipelineStage.Redact | ||
| PipelineStage.ReplaceRoot | ||
| PipelineStage.ReplaceWith | ||
| PipelineStage.Sample | ||
| PipelineStage.Search | ||
| PipelineStage.Set | ||
| PipelineStage.SetWindowFields | ||
| PipelineStage.Skip | ||
| PipelineStage.Sort | ||
| PipelineStage.SortByCount | ||
| PipelineStage.UnionWith | ||
| PipelineStage.Unset | ||
| PipelineStage.Unwind | ||
|
||
export namespace PipelineStage { | ||
export interface AddFields { | ||
/** [`$addFields` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/addFields/) */ | ||
$addFields: Record<string, any> | ||
} | ||
|
||
export interface Bucket { | ||
/** [`$bucket` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/bucket/) */ | ||
$bucket: { | ||
groupBy: any | ||
boundaries: any[] | ||
default?: any | ||
output?: Record<string, { [op in AccumulatorOperator]?: any }> | ||
} | ||
} | ||
|
||
export interface BucketAuto { | ||
/** [`$bucketAuto` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/bucketAuto/) */ | ||
$bucketAuto: { | ||
groupBy: any | ||
buckets: number | ||
output?: Record<string, { [op in AccumulatorOperator]?: any }> | ||
granularity?: 'R5' | 'R10' | 'R20' | 'R40' | 'R80' | '1-2-5' | 'E6' | 'E12' | 'E24' | 'E48' | 'E96' | 'E192' | 'POWERSOF2' | ||
} | ||
} | ||
|
||
export interface CollStats { | ||
/** [`$collStats` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/collStats/) */ | ||
$collStats: { | ||
latencyStats?: { histograms?: boolean } | ||
storageStats?: { scale?: number } | ||
count?: {} | ||
queryExecStats?: {} | ||
} | ||
} | ||
|
||
export interface Count { | ||
/** [`$count` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/count/) */ | ||
$count: string | ||
} | ||
|
||
export interface Facet { | ||
/** [`$facet` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/facet/) */ | ||
$facet: Record<string, FacetPipelineStage[]> | ||
} | ||
|
||
export type FacetPipelineStage = Exclude<PipelineStage, PipelineStage.CollStats | PipelineStage.Facet | PipelineStage.GeoNear | PipelineStage.IndexStats | PipelineStage.Out | PipelineStage.Merge | PipelineStage.PlanCacheStats> | ||
|
||
export interface GeoNear { | ||
/** [`$geoNear` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/geoNear/) */ | ||
$geoNear: { | ||
near: { type: 'Point'; coordinates: [number, number] } | [number, number] | ||
distanceField: string | ||
distanceMultiplier?: number | ||
includeLocs?: string | ||
key?: string | ||
maxDistance?: number | ||
minDistance?: number | ||
query?: AnyObject | ||
spherical?: boolean | ||
uniqueDocs?: boolean | ||
} | ||
} | ||
|
||
export interface GraphLookup { | ||
/** [`$graphLookup` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/graphLookup/) */ | ||
$graphLookup: { | ||
from: string | ||
startWith: any | ||
connectFromField: string | ||
connectToField: string | ||
as: string | ||
maxDepth?: number | ||
depthField?: string | ||
restrictSearchWithMatch?: AnyObject | ||
} | ||
} | ||
|
||
export interface Group { | ||
/** [`$group` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/group) */ | ||
$group: { _id: any } | { [key: string]: { [op in AccumulatorOperator]?: any } } | ||
} | ||
|
||
export interface IndexStats { | ||
/** [`$indexStats` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/indexStats/) */ | ||
$indexStats: {} | ||
} | ||
|
||
export interface Limit { | ||
/** [`$limit` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/limit/) */ | ||
$limit: number | ||
} | ||
|
||
export interface ListSessions { | ||
/** [`$listSessions` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/listSessions/) */ | ||
$listSessions: { users?: { user: string; db: string }[] } | { allUsers?: true } | ||
} | ||
|
||
export interface Lookup { | ||
/** [`$lookup` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/) */ | ||
$lookup: { | ||
from: string | ||
as: string | ||
localField?: string | ||
foreignField?: string | ||
let?: Record<string, any> | ||
pipeline?: Exclude<PipelineStage, PipelineStage.Merge | PipelineStage.Out | PipelineStage.Search>[] | ||
} | ||
} | ||
|
||
export interface Match { | ||
/** [`$match` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/match/) */ | ||
$match: AnyObject | ||
} | ||
|
||
export interface Merge { | ||
/** [`$merge` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/merge/) */ | ||
$merge: { | ||
into: string | { db: string; coll: string } | ||
on?: string | string[] | ||
let?: Record<string, any> | ||
whenMatched?: 'replace' | 'keepExisting' | 'merge' | 'fail' | Extract<PipelineStage, PipelineStage.AddFields | PipelineStage.Set | PipelineStage.Project | PipelineStage.Unset | PipelineStage.ReplaceRoot | PipelineStage.ReplaceWith>[] | ||
whenNotMatched?: 'insert' | 'discard' | 'fail' | ||
} | ||
} | ||
|
||
export interface Out { | ||
/** [`$out` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/out/) */ | ||
$out: string | { db: string; coll: string } | ||
} | ||
|
||
export interface PlanCacheStats { | ||
/** [`$planCacheStats` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/planCacheStats/) */ | ||
$planCacheStats: {} | ||
} | ||
|
||
export interface Project { | ||
/** [`$project` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/project/) */ | ||
$project: { [field: string]: any } | ||
} | ||
|
||
export interface Redact { | ||
/** [`$redact` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/redact/) */ | ||
$redact: any | ||
} | ||
|
||
export interface ReplaceRoot { | ||
/** [`$replaceRoot` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/replaceRoot/) */ | ||
$replaceRoot: { newRoot: any } | ||
} | ||
|
||
export interface ReplaceWith { | ||
/** [`$replaceWith` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/replaceWith/) */ | ||
$replaceWith: any | ||
} | ||
|
||
export interface Sample { | ||
/** [`$sample` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/sample/) */ | ||
$sample: { size: number } | ||
} | ||
|
||
export interface Search { | ||
/** [`$search` reference](https://docs.atlas.mongodb.com/reference/atlas-search/query-syntax/) */ | ||
$search: { | ||
index?: string; | ||
highlight?: { | ||
/** [`highlightPath` reference](https://docs.atlas.mongodb.com/atlas-search/path-construction/#multiple-field-search) */ | ||
path: string | string[] | { value: string, multi: string}; | ||
maxCharsToExamine?: number; | ||
maxNumPassages?: number; | ||
}; | ||
[key: string]: any; | ||
} | ||
} | ||
|
||
export interface Set { | ||
/** [`$set` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/set/) */ | ||
$set: Record<string, any> | ||
} | ||
|
||
export interface SetWindowFields { | ||
/** [`$setWindowFields` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/setWindowFields/) */ | ||
$setWindowFields: { | ||
partitionBy?: any | ||
sortBy?: Record<string, 1 | -1> | ||
output: Record< | ||
string, | ||
{ [op in WindowOperator]?: any } & { | ||
window?: { | ||
documents?: [string | number, string | number] | ||
range?: [string | number, string | number] | ||
unit?: 'year' | 'quarter' | 'month' | 'week' | 'day' | 'hour' | 'minute' | 'second' | 'millisecond' | ||
} | ||
} | ||
> | ||
} | ||
} | ||
|
||
export interface Skip { | ||
/** [`$skip` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/skip/) */ | ||
$skip: number | ||
} | ||
|
||
export interface Sort { | ||
/** [`$sort` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/sort/) */ | ||
$sort: Record<string, 1 | -1 | { $meta: 'textScore' }> | ||
} | ||
|
||
export interface SortByCount { | ||
/** [`$sortByCount` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/sortByCount/) */ | ||
$sortByCount: any | ||
} | ||
|
||
export interface UnionWith { | ||
/** [`$unionWith` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/unionWith/) */ | ||
$unionWith: | ||
| string | ||
| { coll: string; pipeline?: Exclude<PipelineStage, PipelineStage.Out | PipelineStage.Merge>[] } | ||
} | ||
|
||
export interface Unset { | ||
/** [`$unset` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/unset/) */ | ||
$unset: string | string[] | ||
} | ||
|
||
export interface Unwind { | ||
/** [`$unwind` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/unwind/) */ | ||
$unwind: string | { path: string; includeArrayIndex?: string; preserveNullAndEmptyArrays?: boolean } | ||
} | ||
|
||
type AccumulatorOperator = '$accumulator' | '$addToSet' | '$avg' | '$count' | '$first' | '$last' | '$max' | '$mergeObjects' | '$min' | '$push' | '$stdDevPop' | '$stdDevSamp' | '$sum' | ||
|
||
type WindowOperator = '$addToSet' | '$avg' | '$count' | '$covariancePop' | '$covarianceSamp' | '$derivative' | '$expMovingAvg' | '$integral' | '$max' | '$min' | '$push' | '$stdDevSamp' | '$stdDevPop' | '$sum' | '$first' | '$last' | '$shift' | '$denseRank' | '$documentNumber' | '$rank' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters