Home > Software design >  How to get Categories and sub-categories in single API response
How to get Categories and sub-categories in single API response

Time:11-04

I have two collections Categories and Subcategories inside Categories colletion I have an array that is storing an ids of subcategories that are stored in Subcategories collection.Below is my document structure:

Categories collection

{
  id:65,
  title:"Automotive",
  active:true,
  subcategories:[35,28,30]
}

subcategories collection

{
  id:35,
  title:"Automotive technology",
  category_id:65,
  active:true
},
{
  id:28,
  title:"Automotive coatings",
  category_id:65,
  active:true
},
{
  id:30,
  title:"Machinery and equipments",
  category_id:65,
  active:true
}  

AS seen in above collection 3 documents from subcategories collection have been associated with the category document. I want to fetch data in below format on single API hit.

API response should be in below format:

{
 data:{
     category:{
               id:65,
               title:"Automotive",
               subcategories:[{
                                id:35, 
                                name:"Automotive technology",
                              },
                              {
                                id:28, 
                                name:"Automotive coatings",
                              },
                              {
                                id:30, 
                                name:"Machinery and equipments",
                             }]
              },
     category:{
                 id:66,
                 title:"Food",
                 subcategories:[{
                                 id:23, 
                                 name:"Drinks",
                                },
                                {
                                 id:2, 
                                 name:"Additives",
                               }]
               },
   },
   messsage:"Success",
   code:200
}

AS of now I am able to get data in 2 api hits that is like first getting all the categories

 const category = await db.categories.find({});

Then on click of some particular category fetching all the sub categories based on the category id.

const subCategories = await db.SubCategories.find({category_id:id});   

Someone let me know how can I get the above data in desired format in single API hit.Any help would be appreciated.

CodePudding user response:

You need something like this, also, if you use mongoose, you can use .populate()

To format data you can use $project stage in aggregation pipeline or projection in .find()

CodePudding user response:

#If u wanna use mongoose with populate:

CategorySchema:

const CategorySchema= new mongoose.Schema({
  ...
  subCategories: [{ type: Number, ref: 'SubCategory' }],
  ...
});

  • ref content is must be equal to model name like

    module.exports = mongoose.model('SubCategory', SubCategorySchema);

Controller:

const categories = await Armature.find({})
  .populate({
    path: 'subCategories'
  })
  • path content is must be equal to column name

#If you wanna write with mongo query

db.getCollection("categories").aggregate([
    {
       $lookup:
         {
             from: 'subCategories',
             localField: 'subCategories',
             foreignField: 'id',
             as: 'subCategories'
         }
    }
])
  • Related