I have a database in SQL Server which is working fine.
These are my tables:
Categories table
Products table
Supplier table
Ratings table
I am using this query to get required data
select
c.Id, c.CatName,
s.Name as SupplierName,
count(distinct(p.Id)) as ProductCount,
r.Rating
from
Categories c
inner join
Products p on p.CategoryId = c.Id
inner join
Supplier s on s.Id = p.SupplierId
inner join
Ratings r on r.SupplierId = s.Id
Group by
c.Id, c.CatName, s.Name, r.Rating
This query returns this data
This is exactly what I want - but in MongoDb.
I have the same structure of my mongodb collection like the SQL Server tables.
I want the same query in MongoDb...
Here is what i am trying
Catrgories.aggregate([{
$lookup: {
from: 'Products',
localField: 'CategoryId',
foreignField: 'id',
as: 'p'
}
},
{
$lookup: {
from: 'Supplier',
localField: 'SupplierId',
foreignField: 'Id',
as: 's'
}
},
{
$lookup: {
from: 'products',
localField:'supplier',
foreignField:'owner',
as: 'prooo'
},
},
{
$lookup: {
from: 'ratings',
localField: 'SupplierId',
foreignField: 'Id',
as: 'r'
}
},
{
$project: {
"Id":"$c.Id",
"CatName":"$c.CatName"
}
},
])