Hello everyone.
I have a collection of books.
[
{
"id": 1,
"name": "Crime and punishment"
},
{
"id": 2,
"name": "Atomic habits"
},
{
"id": 3,
"name": "Demons"
},
{
"id": 4,
"name": "C# for beginners"
}
]
And I have a shopping list(The id in books is the id of the book in books collection):
[
{
"id": 1,
"userId": 75,
"books": [
{
"id": 1,
"price": 50
},
{
"id": 2,
"price": 20
}
]
},
{
"id": 2,
"userId": 184,
"books": [
{
"id": 3,
"price": 10
},
{
"id": 4,
"price": 99
}
]
}
]
I want a field for each product to inform me if the product is purchased or not.
There I tried to get the shopData list and if length !== 0 => we purchased the book.
{
$lookup: {
from: 'Purchases',
let: { id: "$id",
pipeline: [
{ $match:
{ $expr: {
$and: [
{ $eq: ['$userId', user.id] },
{ $in: ['$books.id', '$id'] }
], } } },
],
as: 'shopData',
},
}
But i always get empty shopData.
Tried through $addFields, to immediately on the database side to calculate whether it is in the lists or not, and not pull the entire list, but also to no avail.
What i’am doing wrong ?