Santhosh
Santhosh

Reputation: 47

aggregate pipeline string forming

if (field) {
    if (field === "3") {
        // count_query_match = { $match:{ $and:[ {partyName:{'$regex' : '^'+search+'$', '$options' : 'i'}} ,{company_id:new mongoose.Types.ObjectId(req.user.company_id)} ]} }
        count_query_match = { $match: { partyName: { '$regex': '^' + search + '$', '$options': 'i' }, company_id: new mongoose.Types.ObjectId(req.user.company_id) } }
    } else if (field === "4") {
        count_query_match = { $match: { pr_phoneNumber: { '$regex': '^' + search + '$', '$options': 'i' } } }
    } else if (field === "5") {
        count_query_match = { $match: { vehicleNo: { '$regex': '^' + search + '$', '$options': 'i' } } }
    }
}

//   count_query_match = {
//     ...count_query_match,company_id:new mongoose.Types.ObjectId(req.user.company_id)
// }

console.log(count_query_match);

count_query_group = {
    $group: {
        // _id : "$partyName",
        _id: null,
        netWeight: { $sum: "$netWeight" },
        netweightXvalue: { $sum: "$netweightXvalue" },
        netValue: { $sum: "$netValue" },
        count: { $sum: 1 }
    }
}

data5 = await Productregistration.aggregate([count_query_match, count_query_group]);

res.status(200)
    .json({
        success: true,
        count: data5.length,
        // data1: data5[0],
        data2: data5
    });

how can i add company_id:new mongoose.Types.ObjectId(req.user.company_id) condition to all aggregate count query match function in common like and condition.

in normal query i will do like this do add common condtions

query = {
    ...query,
    company_id: req.user.company_id
}

Upvotes: 0

Views: 51

Answers (1)

namar sood
namar sood

Reputation: 1590

You can use any of these 2 approaches:

Directly add the common condition as a property to the $match

 if (field) {
    if (field === "3") {
        count_query_match = { $match: { partyName: { '$regex': '^' + search + '$', '$options': 'i' } }}
    } else if (field === "4") {
        count_query_match = { $match: { pr_phoneNumber: { '$regex': '^' + search + '$', '$options': 'i' } } }
    } else if (field === "5") {
        count_query_match = { $match: { vehicleNo: { '$regex': '^' + search + '$', '$options': 'i' } } }
    }
}

count_query_match["$match"].company_id =  company_id: new mongoose.Types.ObjectId(req.user.company_id);

Using another variable match for forming the dynamic query and then spreading it with the common condition

let match;
if (field) {
    if (field === "3") {
        match = { partyName: { '$regex': '^' + search + '$', '$options': 'i' }};
    } else if (field === "4") {
        match = { pr_phoneNumber: { '$regex': '^' + search + '$', '$options': 'i' } };
    } else if (field === "5") {
        match = { vehicleNo: { '$regex': '^' + search + '$', '$options': 'i' } };
    }
}

const count_query_match = {
      $match: {
        ...match,
        company_id:new mongoose.Types.ObjectId(req.user.company_id)
      }
};

Upvotes: 1

Related Questions