I am trying to make API for my invoices data in which there are some boolean values on that basis I want to search those invoices. Like there are some invoice marked as 'payment received' and I want to search those invoice in mongoDB
I am using: Backend - ExpressJS Data Base - MongoDB
Route:
app.get("/api/invoice/search", async (req, res) => {
try {
const invoice = await Invoice.find({
isDispatched: true, // This is dynamic data I want to search this data from req.body, this is // for testing
});
res.status(200).json({
success: true,
totalInvoices: invoice.length,
invoice,
});
} catch (err) {
console.log(err);
}
});
Output, I am getting all the data instead of the those filtered with query.
I want to search multiple fields here I am using $or for this but its not working.
app.get("/api/invoice/search", async (req, res) => {
try {
const invoice = await Invoice.find({
$or: [
{
isDispatched: { $exists: true }, //This has to be dynamic data from req.data
},
{
paymentPending: { $exists: true }, //This has to be dynamic data from req.data
},
],
});
res.status(200).json({
success: true,
totalInvoices: invoice.length,
invoice,
});
} catch (err) {
console.log(err);
}
});
Output is random like sometime I get few data which has both true and false data.
I have tried removing $exists, adding $regex but didn't worked.
I have tried removing $exists, adding $regex but didn't worked.
isDispatchedhas a value oftrue) while the second example is checking for the existence of the field(s)? It is expected thatisDispatched: { $exists: true }will match and return a document that has the value ofisDispatched: falseisDispatched: true, then I am expecting invoices as response only those which hasisDispatched : trueit the data. I am making a search API to search invoices based on these values/data. I want to searchbooleanvalues in my database. As you know$regexdonot work with boolean values so what should I do to search boolean values in mongoDB data base.