Agregation pipeline with Relational Pointer

±-------------------------------------------------------+

| Company class |

±---------±--------------±----------±----------------+
| objectId | category (Category Pointer) | name | subCategory (subCategory Relation) |
±---------±--------------±----------±----------------+

±----------------------------------------------------------------------------------+
| SubCategory class |
±---------±-------------------------±--------------±----------±----------------+
| objectId | name | category (Category Pointer)|
±---------±-------------------------±--------------±----------±----------------+

Relation between of Company and SubCategory is mant-to-many

I want to get all companies with belongs to specific category and match specific subcategory using lookup aggregation query.

Please help me. Thanks all.

Could you please share what you’ve tried so far?

var pipeline = [
{ match: { status: true } },
{ match: { _p_category: ‘Category$’ + categoryId } },
{
project: {
brand: 1,
discount: 1,
logo: 1,
status: 1,
main: 1,
objPointer: { ‘$concat’: [‘Company$’, ‘$_id’] }
}
},
{
lookup: {
from: “CompanyQualification”,
localField: “objPointer”,
foreignField: “_p_company”,
as: “qualifications”
}
},
{
lookup: {
from: “Shop”,
let: { companyID: “$objPointer”, cityID: “City$” + cityId, state: true },
pipeline: [
{
$match:
{
$expr:
{
$and:
[
{ $eq: ["$$companyID", “$_p_company”] },
{ $eq: ["$$cityID", “$_p_city”] },
{ $eq: ["$$state", “$status”] }
]
}
}
}
],
as: “shops”
}
},
{
match: { shops: { $ne: } }
},
{ sort: { main: -1 } },
];

This is the current pipeline, i need to match a specific subCategory in the Company but this is the type Relation not Pointer so i don`t know how can i do it?

In the case of a relation your database have a collection called _Join:subCategory:Company with a owningId field containing the if of the Company object and a relatedId field containing the id of the associated SubCategory object. You need to adjust your aggregate steps accordingly.

But, It is possible to do a lookup with this collections? seems to not work.

Yes. It should be possible.