所以我有扩展名 mongodb paginate v2 的查询,它尝试创建一个具有某些条件的动态查询,但它没有返回我想要的预期结果,这是我的尝试:
async getIBOBHistory(req, res) {
try {
await body("limit")
.exists({ checkFalsy: true })
.withMessage("Limit params must required!")
.run(req);
await body("page")
.exists({ checkFalsy: true })
.withMessage("Limit params must required!")
.run(req);
await body("plant")
.exists({ checkFalsy: true })
.withMessage("Plant params must required!")
.run(req);
await body("zona")
.exists({ checkFalsy: true })
.withMessage("Zona params must required!")
.run(req);
await body("gedung")
.exists({ checkFalsy: true })
.withMessage("Gedung params must required!")
.run(req);
//--------------------------------Validator--------------------------------
const result = validationResult(req);
if (!result.isEmpty()) {
res.status(400).json({
message: "Missing params!",
data: result.array(),
});
return;
}
//--------------------------------PAGINATION--------------------------------
const limit = req.body.limit ? parseInt(req.body.limit) : 15;
const page = req.body.page ? parseInt(req.body.page) : 1;
const order = req.body.order ? req.body.order : { SKU: -1 };
const plant = req.body.plant;
const zona = req.body.zona;
const gedung = req.body.gedung;
console.log(plant);
//--------------------------------QUERY--------------------------------
const options = {
page: page,
limit: limit,
sort: order,
};
let query = [];
if (req.body.search) {
query.push({
$and: [
{
$or: [
{
SKU: { $regex: req.body.search, $options: "i" },
},
{
Material: { $regex: req.body.search, $options: "i" },
},
{
Material_Description: {
$regex: req.body.search,
$options: "i",
},
},
{
Type: { $regex: req.body.search, $options: "i" },
},
{
Lokasi: { $regex: req.body.search, $options: "i" },
},
{
Expired_Date: { $regex: req.body.search, $options: "i" },
},
{
Batch: { $regex: req.body.search, $options: "i" },
},
{
PO_Number: { $regex: req.body.search, $options: "i" },
},
{
Channel: { $regex: req.body.search, $options: "i" },
},
],
},
{
$and: [
{
Plant: plant,
},
{
$and: [
{
Zona: zona,
},
{
Gedung: gedung,
},
],
},
],
},
],
});
} else {
query.push({
$and: [
{
Plant: plant,
},
{
$and: [
{
Zona: zona,
},
{
Gedung: gedung,
},
],
},
],
});
}
//--------------------------------GET DATA--------------------------------
const data = await TransactionIBOB.paginate(
query[0] ? query[0] : {},
options
);
//--------------------------------RETURN--------------------------------
const new_data = data.docs.map((item) => {
console.log(item);
let new_item = {};
new_item._id = item._id;
new_item.transactionId = item.transactionId;
new_item.Material = item.Material;
new_item.Material_Description = item.Material_Description;
new_item.Type = item.Type;
new_item.Lokasi = item.Lokasi;
new_item.Expired_Date = item.Expired_Date;
new_item.Batch = item.Batch;
new_item.Gedung = item.Gedung;
new_item.Zona = item.Zona;
new_item.Plant = item.Plant;
new_item.PO_Number = item.PO_Number;
new_item.InBound = item.InBound;
new_item.OutBound = item.OutBound;
new_item.Total = item.Total;
new_item.Shift = item.Shift;
new_item.Channel = item.Channel ? item.Channel : "Manual";
new_item.createdAt = converterDate(item.createdAt, "Asia/Jakarta");
new_item.updatedAt = converterDate(item.updatedAt, "Asia/Jakarta");
return new_item;
});
data.docs = new_data;
if (!data) {
const error = new Error("No data found");
error.status = 404;
throw error;
}
//--------------------------------RETURN--------------------------------
return res.status(200).json({
status: 200,
message: "Success Retrieving Data",
data: data,
});
} catch (err) {
//--------------------------------ERROR--------------------------------
next(err);
if (!err.status) {
err.status = 500;
}
return res.status(err.status).json({
status: err.status,
message: err.message,
data: err.data ? err.data : [],
});
}
}
结果应该只返回确切的植物、gedung、zona 以及搜索值,但它返回所有而不是我的预期结果,有人可以指出我在这里做错了什么,或者对此有任何帮助.....至少解释一下如何使用它