Мне нужно получить данные между диапазонами, если упомянуто, если не упомянуто, мне нужно вернуть все данные в моей коллекции. Я уже использую метод регулярного выражения, чтобы найти данные, упомянутые в ключе поиска param.please помогите мне с эта проблема .... Параметры, которые я передаю: {"searchKey": "", "status": "In-Progress", "defaultAccountId": 1000, "fromDate": "2020-03-18T05: 09: 22.255 Z "," toDate ":" 2021-03-18T05: 09: 22.255Z "}, даже если я пропускаю диапазоны дат, я просто получаю все данные
async function getAll(userParam) {
const myCustomLabels = {
totalDocs: 'totalData',
docs: 'data',
hasNextPage: 'loadMore',
totalPages: "pages",
hasPrevPage: false,
limit: false,
pagingCounter: false,
prevPage: false,
page: false
};
var start = new Date(userParam.fromDate);
var end = new Date(userParam.toDate);
const page = userParam.page
const options = {
page: parseInt(page),
sort: {
_id: -1
},
limit: 50,
customLabels: myCustomLabels
};
if (userParam.status == 'In-Progress') {
var userss = User.aggregate([{ $match: { status: 'In-Progress' } },
{
$match:
{
$or: [
{ 'No': { "$regex": userParam.searchKey, "$options": "i" } },
{ 'grnNo': { "$regex": userParam.searchKey, "$options": "i" } },
{ "party": { "$regex": userParam.searchKey, "$options": "i" } },
{ "buyer": { "$regex": userParam.searchKey, "$options": "i" } },
{ "lotNo": { "$regex": userParam.searchKey, "$options": "i" } },
{ "orderNo": { "$regex": userParam.searchKey, "$options": "i" } },
{ "manualDcNo": { "$regex": userParam.searchKey, "$options": "i" } },
{ "dyeingDcNo": { "$regex": userParam.searchKey, "$options": "i" } },
{ "dyeingName": { "$regex": userParam.searchKey, "$options": "i" } },
{ "partyDcNo": { "$regex": userParam.searchKey, "$options": "i" } },
{ "partyDcRolls": { "$regex": userParam.searchKey, "$options": "i" } },
{ "weight": { "$regex": userParam.searchKey, "$options": "i" } },
{ "vehicleNo": { "$regex": userParam.searchKey, "$options": "i" } },
{ "driverName": { "$regex": userParam.searchKey, "$options": "i" } },
{ "deliveryReference": { "$regex": userParam.searchKey, "$options": "i" } },
{ "deliverTo": { "$regex": userParam.searchKey, "$options": "i" } },
{ "reference": { "$regex": userParam.searchKey, "$options": "i" } },
{ "referName": { "$regex": userParam.searchKey, "$options": "i" } },
{ "fabricArray.fabricName": { "$regex": userParam.searchKey, "$options": "i" } },
{ "fabricArray.color": { "$regex": userParam.searchKey, "$options": "i" } },
{ "fabricArray.dia": { "$regex": userParam.searchKey, "$options": "i" } },
{ "fabricArray.rolls": { "$regex": userParam.searchKey, "$options": "i" } },
{ "fabricArray.weight": { "$regex": userParam.searchKey, "$options": "i" } },
{ "fabricArray.template": { "$regex": userParam.searchKey, "$options": "i" } },
{ "fabricArray.remarks": { "$regex": userParam.searchKey, "$options": "i" } },
{ "orderHistory.type": { "$regex": userParam.searchKey, "$options": "i" } },
{ "orderHistory.no": { "$regex": userParam.searchKey, "$options": "i" } },
{ "orderHistory.rolls": { "$regex": userParam.searchKey, "$options": "i" } },
{ "orderHistory.weight": { "$regex": userParam.searchKey, "$options": "i" } },
{ "processArray.processName": { "$regex": userParam.searchKey, "$options": "i" } },
{ "processArray.productionWeight": { "$regex": userParam.searchKey, "$options": "i" } },
{ "processArray.loss": { "$regex": userParam.searchKey, "$options": "i" } },
{ "averageWeightLoss": { "$regex": userParam.searchKey, "$options": "i" } },
{ "averageWeightGain": { "$regex": userParam.searchKey, "$options": "i" } },
{ "createdByName": { "$regex": userParam.searchKey, "$options": "i" } },
{ "createdById": { "$regex": userParam.searchKey, "$options": "i" } },
{ "partyDcRolls": { "$gte": { $toInt: userParam.searchKey }, "$lte": { $toInt: userParam.searchKey } } },
{ "weight": { "$gte": { $toDecimal: userParam.searchKey }, "$lte": { $toDecimal: userParam.searchKey } } },
{ "createdDate": { "$gte": start, "$lte": end } }
],
$and: [
{ "defaultAccountId": { "$gte": parseInt(userParam.defaultAccountId), "$lte": parseInt(userParam.defaultAccountId) } },
],
// $or: []
}}, { $project: { no: '$no', party: '$party', grnNo: '$grnNo', date: '$date', weight: '$weight', partyDcNo: '$partyDcNo', partyDcDate: '$partyDcDate', dyeingDcNo: '$dyeingDcNo', dyeingDcDate: '$dyeingDcDate', createdByName: '$createByName', createdDate: '$createdDate' } }
]);
var usersss = User.aggregatePaginate(userss, options)
return await (usersss)
}
else if (userParam.status == 'Completed') {
var userss = User.aggregate([{ $match: { status: 'Completed' } },
{
$match:
{
$or: [
{ 'No': { "$regex": userParam.searchKey, "$options": "i" } },
{ 'grnNo': { "$regex": userParam.searchKey, "$options": "i" } },
{ "party": { "$regex": userParam.searchKey, "$options": "i" } },
{ "buyer": { "$regex": userParam.searchKey, "$options": "i" } },
{ "lotNo": { "$regex": userParam.searchKey, "$options": "i" } },
{ "orderNo": { "$regex": userParam.searchKey, "$options": "i" } },
{ "manualDcNo": { "$regex": userParam.searchKey, "$options": "i" } },
{ "dyeingDcNo": { "$regex": userParam.searchKey, "$options": "i" } },
{ "dyeingName": { "$regex": userParam.searchKey, "$options": "i" } },
{ "partyDcNo": { "$regex": userParam.searchKey, "$options": "i" } },
{ "partyDcRolls": { "$regex": userParam.searchKey, "$options": "i" } },
{ "weight": { "$regex": userParam.searchKey, "$options": "i" } },
{ "vehicleNo": { "$regex": userParam.searchKey, "$options": "i" } },
{ "driverName": { "$regex": userParam.searchKey, "$options": "i" } },
{ "deliveryReference": { "$regex": userParam.searchKey, "$options": "i" } },
{ "deliverTo": { "$regex": userParam.searchKey, "$options": "i" } },
{ "reference": { "$regex": userParam.searchKey, "$options": "i" } },
{ "referName": { "$regex": userParam.searchKey, "$options": "i" } },
{ "fabricArray.fabricName": { "$regex": userParam.searchKey, "$options": "i" } },
{ "fabricArray.color": { "$regex": userParam.searchKey, "$options": "i" } },
{ "fabricArray.dia": { "$regex": userParam.searchKey, "$options": "i" } },
{ "fabricArray.rolls": { "$regex": userParam.searchKey, "$options": "i" } },
{ "fabricArray.weight": { "$regex": userParam.searchKey, "$options": "i" } },
{ "fabricArray.template": { "$regex": userParam.searchKey, "$options": "i" } },
{ "fabricArray.remarks": { "$regex": userParam.searchKey, "$options": "i" } },
{ "orderHistory.type": { "$regex": userParam.searchKey, "$options": "i" } },
{ "orderHistory.no": { "$regex": userParam.searchKey, "$options": "i" } },
{ "orderHistory.rolls": { "$regex": userParam.searchKey, "$options": "i" } },
{ "orderHistory.weight": { "$regex": userParam.searchKey, "$options": "i" } },
{ "processArray.processName": { "$regex": userParam.searchKey, "$options": "i" } },
{ "processArray.productionWeight": { "$regex": userParam.searchKey, "$options": "i" } },
{ "processArray.loss": { "$regex": userParam.searchKey, "$options": "i" } },
{ "averageWeightLoss": { "$regex": userParam.searchKey, "$options": "i" } },
{ "averageWeightGain": { "$regex": userParam.searchKey, "$options": "i" } },
{ "createdByName": { "$regex": userParam.searchKey, "$options": "i" } },
{ "createdById": { "$regex": userParam.searchKey, "$options": "i" } },
{ "partyDcRolls": { "$gte": parseInt(userParam.searchKey), "$lte": parseInt(userParam.searchKey) } },
{ "weight": { "$gte": { $toDecimal: userParam.searchKey }, "$lte": { $toDecimal: userParam.searchKey } } },
// { "weight":{"$gte": parseDecimalNumber(userParam.searchKey),"$lte":parseDecimalNumber(userParam.searchKey)}}
],
$and: [
{ "defaultAccountId": { "$gte": parseInt(userParam.defaultAccountId), "$lte": parseInt(userParam.defaultAccountId) } },
],
$or: [{ "createdDate": { "$gte": start, "$lte": end } }]
}
}, { $project: { no: '$no', party: '$party', grnNo: '$grnNo', date: '$date', weight: '$weight', partyDcNo: '$partyDcNo', partyDcDate: '$partyDcDate', dyeingDcNo: '$dyeingDcNo', dyeingDcDate: '$dyeingDcDate', createdByName: '$createByName', createdDate: '$createdDate' } }
]);
var usersss = User.aggregatePaginate(userss, options)
return await (usersss)
}
}