Home > OS >  MongoDB query to send response of all fields in one json format
MongoDB query to send response of all fields in one json format

Time:06-15

I have to send the response in one JSON format in which three brand names along with three manufacturer names are there. I paste a sample example in last.

This is my code -

getFilterData: function (req, res, next) {
        console.log('Itemfilterlist getAll ');
        let filterlist = []
        let agg = [  {'$match': {'generic_name': req.body.generic_name, 'form_name': req.body.form_name, 'dose_size': req.body.dose_size}}, 
        {'$sort': {'mrp': 1}}, 
        {'$limit': 3},
        {'$group': {_id:'$id',  "generic_name": {$first:"$generic_name"},
        "form_name": {$first:"$form_name"},
        "dose_size": {$first:"$dose_size"},
        "subcategory_name": {$first:"$subcategory_name"},
        brand1: {$push: { "brand_name": "$brand_name", "manufacturer_name": "$manufacturer_name"}}}}
       ]
        let i = 0;
        MasterModel.aggregate(agg,  function (err, arrList) {
            if (err) {
                next(err);
            }
            else {   
                for (let h of arrList) {
                    h.id =   i            
                    filterlist.push(h);
                }                                                                       

                res.json({ filterList: filterlist })
                
            }
        });
    },

I use the group aggregation with push accumulator to show the data, With the help of a group query but I get this JSON data response:-

 "filterList": [
        {
            "_id": null,
            "generic_name": "Ademetionine",
            "form_name": "Tablets-MD",
            "dose_size": "400MG",
            "subcategory_name": "Others",
            "brand1": [
                {
                    "brand_name": "CARTISURE 400 MG TAB",
                    "manufacturer_name": "Zoic Lifesciences"
                },
                {
                    "brand_name": "ADENORICH 400 MG TAB",
                    "manufacturer_name": "Future Pharma Pvt Ltd"
                },
                {
                    "brand_name": "ENSAME 400 TAB",
                    "manufacturer_name": "ICARUS HEALTHCARE"
                }
            ],
            "id": 1
        }
    ]

But I want my JSON format to be like this:-

{
    id: 1,
    molecule: 'Heparin',
    form: 'Ointments',
    strength: '50IU',
    sub_category: 'CARDIO VASCULAR SYSTEM',
    brand_name_1: 'HEPTRAL(Abbott)',
    brand_name_2: 'HYSAM(Alkem)',
    brand_name_3: 'THROMBOMARK(Mankind)',
  }

CodePudding user response:

You can add a $projection step to your aggregation pipeline. Something like:

db.collection.aggregate([
  {
    $project: {
      brand_name_1: {
        $concat: [
          {$arrayElemAt: ["$brand1.brand_name", 0]}, "(",
          {$arrayElemAt: ["$brand1.manufacturer_name", 0]}, ")"
        ]
      },
      brand_name_2: {
        $concat: [
          {$arrayElemAt: ["$brand1.brand_name", 1]}, "(",
          {$arrayElemAt: ["$brand1.manufacturer_name", 1]}, ")"
        ]
      },
      brand_name_3: {
        $concat: [
          {$arrayElemAt: ["$brand1.brand_name", 2]}, "(",
          {$arrayElemAt: ["$brand1.manufacturer_name", 2]}, ")"
        ]
      },
      sub_category: "$subcategory_name",
      strength: "$dose_size",
      form: "$form_name",
      molecule: "$generic_name",
      id: 1,
      _id: 0
    }
  }
])

See how it works on the playground example

  • Related