Mongoose 6 $group with Typescript -> Property '_id' 与索引签名不兼容

Posted

技术标签:

【中文标题】Mongoose 6 $group with Typescript -> Property \'_id\' 与索引签名不兼容【英文标题】:Mongoose 6 $group with Typescript -> Property '_id' is incompatible with index signatureMongoose 6 $group with Typescript -> Property '_id' 与索引签名不兼容 【发布时间】:2022-01-21 12:37:12 【问题描述】:

我正在从 js 迁移到 ts 以及从 mongoose 5.8 到 6.1 以下代码之前运行良好

let getActionsTakenByApp = (store_url: string) => 
    return AppAction.aggregate([
        
            $match:  store_url 
        ,
        
            $group: 
                _id: 
                    'action_type': '$action_type'
                ,
                count: 
                    $sum: 1
                
            
        
    ]);

但是现在打字稿现在显示这个错误。

Type ' _id:  action_type: string; ; count:  $sum: number; ; ' is not assignable to type ' [key: string]:  $count?: any; $accumulator?: any; $addToSet?: any; $avg?: any; $first?: any; $last?: any; $max?: any; $mergeObjects?: any; $min?: any; $push?: any; $stdDevPop?: any; $stdDevSamp?: any; $sum?: any; ; _id: any; '.   Property '_id' is incompatible with index signature.
    Type ' action_type: string; ' is not assignable to type ' $count?: any; $accumulator?: any; $addToSet?: any; $avg?: any; $first?: any; $last?: any; $max?: any; $mergeObjects?: any; $min?: any; $push?: any; $stdDevPop?: any; $stdDevSamp?: any; $sum?: any; '.
      Object literal may only specify known properties, and ''action_type'' does not exist in type ' $count?: any; $accumulator?: any; $addToSet?: any; $avg?: any; $first?: any; $last?: any; $max?: any; $mergeObjects?: any; $min?: any; $push?: any; $stdDevPop?: any; $stdDevSamp?: any; $sum?: any; '.ts(2322)

我还是打字稿的新手,并试图找出问题所在。任何帮助,将不胜感激!谢谢!!

【问题讨论】:

请查看:github.com/Automattic/mongoose/issues/11059 【参考方案1】:

这是 6.1.0 中引入的bug

related PR 合并发布后会修复。

在此期间,您可以降级到较低版本。

【讨论】:

以上是关于Mongoose 6 $group with Typescript -> Property '_id' 与索引签名不兼容的主要内容,如果未能解决你的问题,请参考以下文章

Mongoose.aggregate(pipeline) 使用 $unwind、$lookup、$group 链接多个集合

SELECT COUNT with Group BY 仅返回值为 2

当使用$ project,$ frequency和$ group时,Mongoose按日期排序

Mongoose:.populate on key with white space 不起作用

Mongoose with Authentication 导致超时

Mongoose with Authentication 导致超时