Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Mongodb aggregation response

I have a Property controller :

//Get Properties By Type
const getPropertiesByType = async (req: Request, res: Response) => {
    const { cities, type } = req.query
    const citiesArr = typeof cities === 'string' ? cities.split(',') : []
    try {
        const property = await Promise.all(
            citiesArr?.map((item) => {
                return PropertyModel.aggregate([
                    {
                        $match: { city: item, propertyType: type },
                    },
                    {
                        $project: {
                            _id: 0,
                            city: 1,
                            country: 1,
                            cityPhoto: 1,
                        },
                    },
                    {
                        $group: {
                            _id: '$city',
                            country: { $first: '$country' },
                            totalProperties: { $sum: 1 },
                            cityPhoto: { $first: '$cityPhoto' },
                        },
                    },
                ])
            })
        )
        res.status(201).json(property)
    } catch (error) {
        console.log(error)
        res.status(400).json(error)
    }
} 

in Postman i am getting this reponse :

[
    [
        {
            "_id": "Davenport",
            "country": "United States",
            "totalProperties": 1,
            "cityPhoto": "https://"
        }
    ],
    [
        {
            "_id": "Destin",
            "country": "United States",
            "totalProperties": 1,
            "cityPhoto": "https://"
        }
    ],
] 

Problem is, that i get all objects in arrays, but i want instead get in response all objects in one main array. Also, can i do that without aggregation pipeline?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use array.flatMap() once you receive aggregation result:

const property = [
    [
        {
            "_id": "Davenport",
            "country": "United States",
            "totalProperties": 1,
            "cityPhoto": "https://"
        }
    ],
    [
        {
            "_id": "Destin",
            "country": "United States",
            "totalProperties": 1,
            "cityPhoto": "https://"
        }
    ],
] ; // your aggregation output

const response = property.flatMap(x => x);
console.log(response);

The reason you can’t do it directly in aggregation pipeline is you are using Promise.all which returns an array and each .aggregate() also returns an array so you will get an array of arrays anyway.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading