'MongoDB Node.js aggregation too slow for function

i hope this question is not answered too many times.

However my problem is that i need to extract objects from an array inside a document. The way i have solved this is with the aggregation pipeline builder integrated in MongoDB, with this i get the correct result.

But when i implemented this in the API i am building the data does not return with the request. So i suspect the operation is too slow or something along those lines.

My code looks like this:

const searchIngredientInInventory = async (req: Request, res: Response) => {
    const dbConn = await getDb();

    const aggDoc = [
        {
          '$match': {
            'type': {
              '$ne': 'shoppingList'
            }
          }
        }, {
          '$unwind': {
            'path': '$container'
          }
        }
      ]
    
    dbConn.collection(coll).aggregate(aggDoc).toArray((err, result) => {
        if (err) {
            res.status(400).send(err);
            console.log(err)
        } else {
            res.status(200).send(result);
            }
        })
}

And the database has this structure:

Cluster
  Database
    Collection
      Doc1
        Array with objects
      Doc2
        Array with objects

The collection is not large, its 5 documents and 36 kilobytes.

The return i get is empty array: []

Does anybody have any idea what i am doing wrong?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source