Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB aggregation with typescript

I have a User model and I want to perform generic aggregation. mean any array of object I pass to this function it executes.

this is the sample of getUser function

public async getUser(aggregate: object): Promise<ResponseDTO> {
     let response = {} as ResponseDTO;
     const [err, user] = await To(User.aggregate(aggregate));
     if (user) {
         response = { success: true, data: user, message: "User fround" };
     }
     else
         response = { success: false, message: "User not fround" };
     return response;
}

and I pass this as a Parameter

const query = [
  {
     $match: {
        name:"Jon"
     }
  },
  {
    $project:{
      _id:1  
    }
  }
]
const userRes = await getUser(query);

But I'm not able to run the program it's giving me syntax error on getUser function

*(method) Model<any, any, any>.aggregate(pipeline?: any[] | undefined): Aggregate<any[]> (+1 overload)

Argument of type 'Aggregate<any[]>' is not assignable to parameter of type 'Promise'. Type 'Aggregate<any[]>' is missing the following properties from type 'Promise': [Symbol.toStringTag], finally*

I tried to change object to any, Array or Array in getUser parameter

here is the SS of the Error enter image description here

PS: I'm using node with typescript and IDE is VSCode

like image 977
Fazal Avatar asked Apr 11 '26 06:04

Fazal


1 Answers

Mongoose provides PipelineStage interface for pipes We can use that right away

import { PipelineStage } from "mongoose";

public async getUser(aggregate: PipelineStage[]):
Promise<ResponseDTO> {
     let response = {} as ResponseDTO;
     const [err, user] = await User.aggregate(aggregate);
     if (user) {
         response = { success: true, data: user, message: "User found" };
     }
     else
         response = { success: false, message: "User not found" };
     return response;
}
like image 199
Mohamed Swadique K Avatar answered Apr 12 '26 19:04

Mohamed Swadique K



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!