0

I have 3 collections that are referred to each other such that

A -> B -> C

I want to filter data after matching that with B and from B to C

Collection 1

Products: [{
_id:ObjectId(),
name:"product1",
productCatalogue:[reference to productCatalogue collection]
},....]

Collection 2

productCatalogue: [{
_id:ObjectId(),
name:"catelgoue1",
category:{
  cat:[reference to category table],
  sub1:[reference to category table],
  sub2:[reference to category table]
}
},...]

Collection 3

category: [{
  _id:ObjectId(),
  name:"cat1",
  type:"parent"
},....]

I want to filter data such that products having catalog:catelgoue1 and category: cat1 will be filtered using aggregation.

  • @turivishal it does but can you explain `name: { $first: "$name" }, address: { $push: "$address" }` what's the role of $first here and $name represent what under approved answer – Mukul Juneja Dec 22 '20 at 18:56
  • Refer the [$group](https://docs.mongodb.com/manual/reference/operator/aggregation/group/index.html) stage and [$first](https://docs.mongodb.com/manual/reference/operator/aggregation/first/index.html) operator and [$push](https://docs.mongodb.com/manual/reference/operator/aggregation/push/index.html) operator. – turivishal Dec 23 '20 at 04:19

1 Answers1

0

you can using before every lookup for $match example code:

const orderProcess = await Order.aggregate([
{
    $match: {
        userId: userId
    }
},
{
    $lookup: {
        from: "products",
        localField: "product_id",
        foreignField: "_id",
        as: "product",                 
    }
},
{
    $unwind: "$product"
},
{
    $lookup: {
        from: "prices",
        localField: "product_id", // $product._id => like top lookup data
        foreignField: "productId", // $product._id => like top lookup data
        as: "price"
    },
},
{
    $unwind: "$price"
},
{
    $project:{
        // response all item
    }
}])
Taner Akhan
  • 138
  • 1
  • 7
  • I have to put the aggregate query on the product schema. I'll dereference catalog and then dereference category from catalog schema to find my answer. Your reply is not giving the desired result. Thank you! – Mukul Juneja Dec 21 '20 at 12:12