[mongodb] Allow optional fields in $inc and $mul (#42821)

This commit is contained in:
Andrew Casey
2020-03-04 10:42:04 -08:00
committed by GitHub
parent 04aa80e248
commit aac1184b61
2 changed files with 6 additions and 2 deletions

View File

@@ -28,6 +28,7 @@
// Alberto Silva <https://github.com/albertossilva>
// Rauno Viskus <https://github.com/Rauno56>
// Piotr Błażejewicz <https://github.com/peterblazejewicz>
// Linus Unnebäck <https://github.com/LinusU>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 3.0
@@ -1279,10 +1280,10 @@ export type PullAllOperator<TSchema> = ({
export type UpdateQuery<TSchema> = {
/** https://docs.mongodb.com/manual/reference/operator/update-field/ */
$currentDate?: OnlyFieldsOfType<TSchema, Date, true | { $type: 'date' | 'timestamp' }>;
$inc?: OnlyFieldsOfType<TSchema, number>;
$inc?: OnlyFieldsOfType<TSchema, number | undefined>;
$min?: MatchKeysAndValues<TSchema>;
$max?: MatchKeysAndValues<TSchema>;
$mul?: OnlyFieldsOfType<TSchema, number>;
$mul?: OnlyFieldsOfType<TSchema, number | undefined>;
$rename?: { [key: string]: string };
$set?: MatchKeysAndValues<TSchema>;
$setOnInsert?: MatchKeysAndValues<TSchema>;

View File

@@ -17,6 +17,7 @@ async function run() {
interface TestModel {
stringField: string;
numberField: number;
optionalNumberField?: number;
dateField: Date;
otherDateField: Date;
oneMoreDateField: Date;
@@ -44,6 +45,7 @@ async function run() {
// buildUpdateQuery({ $currentDate: { stringField: true } }); // stringField is not a date Field
buildUpdateQuery({ $inc: { numberField: 1 } });
buildUpdateQuery({ $inc: { optionalNumberField: 1 } });
buildUpdateQuery({ $inc: { 'dot.notation': 2 } });
buildUpdateQuery({ $inc: { 'subInterfaceArray.$': -10 } });
buildUpdateQuery({ $inc: { 'subInterfaceArray.$[bla]': 40 } });
@@ -68,6 +70,7 @@ async function run() {
// buildUpdateQuery({ $min: { numberField: 'a' } }); // Matches the type of the keys
buildUpdateQuery({ $mul: { numberField: 1 } });
buildUpdateQuery({ $mul: { optionalNumberField: 1 } });
buildUpdateQuery({ $mul: { 'dot.notation': 2 } });
buildUpdateQuery({ $mul: { 'subInterfaceArray.$': -10 } });
buildUpdateQuery({ $mul: { 'subInterfaceArray.$[bla]': 40 } });