Issue
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?
Solution
The problem was in the aggregation document, or at least the problem stopped occurring when I updated it.
The new aggregation document looks like this now:
const aggDoc = [
{
'$match': {
'type': {
'$ne': 'shoppingList'
}
}
}, {
'$unwind': {
'path': '$container'
}
}, {
'$project': {
'item': '$container.item',
'expirationDate': '$container.expirationDate'
}
}, {
'$match': {
'item': {
'$regex': ingredient
}
}
}
]
Answered By - Hevos93
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.