简体   繁体   English

Mongodb/Mongoose 类型聚合不兼容

[英]Mongodb/Mongoose types aggregation incompatibility

After migrating nodejs server to a newer version of mongoose, I encountered a weird typescript error referring to $sort aggregation.将 nodejs 服务器迁移到较新版本的 mongoose 后,我遇到了一个奇怪的 typescript 错误,涉及 $sort 聚合。

My versions我的版本

dependencies "mongoose": "^6.2.10",依赖项“猫鼬”:“^6.2.10”,

Dev dependencies "@types/mongoose": "^5.11.97", "typescript": "^4.6.3"开发依赖 "@types/mongoose": "^5.11.97", "typescript": "^4.6.3"

I simplify code.我简化代码。

export const timetableTest = (group: string): Promise<IGroupTimetableEvents[]> => {
    const agg = [{ $match: { group } }, { $sort: { reference: 1 } }];
    const result = GroupEvent.aggregate(agg).exec();
    return result;
};

GroupEvent.aggregate(agg) does not accept agg variable, I see the following message. GroupEvent.aggregate(agg) 不接受agg变量,我看到以下消息。

Argument of type '({ $match: { group: string; }; $sort?: undefined; } | { $sort: { reference: number; }; $match?: undefined; })[]' is not assignable to parameter of type 'PipelineStage[]'. '({ $match: { group: string; }; $sort?: undefined; } | { $sort: { reference: number; }; $match?: undefined; })[]' 类型的参数不可分配给“PipelineStage[]”类型的参数。 Type '{ $match: { group: string;输入'{ $match: { group: string; }; }; $sort?: undefined; $排序?:未定义; } | } | { $sort: { reference: number; { $排序:{ 参考:数字; }; }; $match?: undefined; $匹配?:未定义; }' is not assignable to type 'PipelineStage'. }' 不可分配给类型 'PipelineStage'。 Type '{ $sort: { reference: number;输入 '{ $sort: { 参考:数字; }; }; $match?: undefined; $匹配?:未定义; }' is not assignable to type 'PipelineStage'. }' 不可分配给类型 'PipelineStage'。 Type '{ $sort: { reference: number;输入 '{ $sort: { 参考:数字; }; }; $match?: undefined; $匹配?:未定义; }' is not assignable to type 'Sort'. }' 不可分配给类型 'Sort'。 Types of property '$sort' are incompatible.属性“$sort”的类型不兼容。 Type '{ reference: number;输入'{参考:数字; }' is not assignable to type 'Record<string, 1 | }' 不可分配给类型 'Record<string, 1 | -1 | -1 | { $meta: "textScore"; { $元:“textScore”; }>'. }>'。 Property 'reference' is incompatible with index signature.属性“引用”与索引签名不兼容。 Type 'number' is not assignable to type '1 |类型“数字”不可分配给类型“1 | -1 | -1 | { $meta: "textScore"; { $元:“textScore”; }'.ts(2345) }'.ts(2345)

I see the issue is coming from TS, nevertheless, I provide 1 (number) which looks like is not accepted.我看到问题来自 TS,不过,我提供了 1(数字),看起来不被接受。

Type 'number' is not assignable to type '1 |类型“数字”不可分配给类型“1 | -1 | -1 | { $meta: "textScore"; { $元:“textScore”; }' }'

Before migration, I did not encounter this error and compilation/code execution was fine.在迁移之前,我没有遇到这个错误,编译/代码执行都很好。

Please, let me know what am I am missing.请让我知道我错过了什么。

I'm currently facing the same issue and as a workaround I came up with this:我目前面临同样的问题,作为一种解决方法,我想出了这个:

const sort: Record<string, | 1 | -1 | {$meta: "textScore"}> = { reference: 1 };

export const timetableTest = (group: string): Promise<IGroupTimetableEvents[]> => {
    const agg = [{ $match: { group } }, { $sort: sort }];
    const result = GroupEvent.aggregate(agg).exec();
    return result;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM