【发布时间】:2020-07-27 00:46:20
【问题描述】:
我的查询在 RoboMongo 中运行良好,但是当我在我的 nodeJs 环境中构建相同的查询时,我遇到了异常。
下面是我的代码
存储库函数
public find = async (account: any): Promise<any> => {
return PromoTypes.aggregate(
{
$match: { account: { $in: account } }
},
{
$group: {
_id: '$account',
data: {
$push: { _id: '$_id', description: '$description', iteration: '$iteration' }
}
}
}
);
};
调用上述 repo 的服务文件。功能
AccountService.ts
public accountService = async (accountid: string): Promise<any> => {
//accounttypes is an array constructed to pass to the repo, it has more business logic about construction which I have not included in this code. But the object would be as below
accounttypes = ["first,second"]
let accountDetails = await AccountRepo.find(accounttypes);
}
以下是我遇到的错误。
Argument of type '{ $match: { account: { $in: any; }; }; }' is not assignable to parameter of type 'any[]'. Object literal may only specify known properties, and '$match' does not exist in type 'any[]'.ts(2345)
我尝试了什么
我尝试将承诺类型更改为以下类型
1. public accountService = async (accountid: string): Promise<any[]> =>{
2. public accountService = async (accountid: string): Promise<Array<Object>> => {
3. public accountService = async (accountid: string): Promise<Object> => {
以上方法都不能解决问题。
【问题讨论】:
-
mongoosejs.com/docs/api/aggregate.html 聚合函数需要一个对象数组
-
将您的聚合查询包装到
[...]
标签: node.js mongodb typescript mongoose types