Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract PipelineStage typings from index.d.ts #11368

Merged
merged 8 commits into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"docs",
"tools",
"dist",
"website.js",
"test/files/*",
"benchmarks"
],
Expand All @@ -22,6 +23,8 @@
"@typescript-eslint"
],
"rules": {
"@typescript-eslint/triple-slash-reference": "off",
"spaced-comment": ["error", "always", { "markers": ["/"] }],
Uzlopak marked this conversation as resolved.
Show resolved Hide resolved
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/ban-types": "off",
"@typescript-eslint/no-unused-vars": "off",
Expand Down
120 changes: 120 additions & 0 deletions test/types/PipelineStage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
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' } }
}
}
}
]
}
};

/**
* $unset
*
* @see https://docs.mongodb.com/manual/reference/operator/aggregation/unset/
*/
const unset1 = { $unset: '<field.nestedfield>' };
const unset2 = { $unset: ['isbn', 'copies'] };
const unset3 = { $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 } };
Loading