1

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.

3
  • okay, what do you expect the response to be ? Commented Dec 15, 2022 at 20:49
  • Why does the first example check the value of the field (e.g. that isDispatched has a value of true) while the second example is checking for the existence of the field(s)? It is expected that isDispatched: { $exists: true } will match and return a document that has the value of isDispatched: false Commented Dec 15, 2022 at 21:17
  • @unhackit I am expecting those invoices as response which has these values in it. Example: I am searching for those invoices which has isDispatched: true, then I am expecting invoices as response only those which has isDispatched : true it the data. I am making a search API to search invoices based on these values/data. I want to search boolean values in my database. As you know $regex donot work with boolean values so what should I do to search boolean values in mongoDB data base. Commented Dec 16, 2022 at 21:40

1 Answer 1

0

My understanding of the question:

The result of the query sometimes returns values where isDispatched data exists and paymentPending does not. Likewise, with paymentPending data existing and isDispatched data not.

Based on this understanding there might be two problems.

First Possible Issue:

If the above is correct it looks like you aren't really trying to use an $or. Try using the following as your query to see if your results are as expected.

{
    isDispatched: { $exists: true },
    paymentPending: { $exists: true }
}

Explanation: $or is going to return any values that match either case.

Second Possible Issue:

The $exists only checks that the value is in the document; it will match a null value. So, you may be expecting null fields to not exist.

{
    isDispatched: { $exists: true, $ne: null },
    paymentPending: { $exists: true, $ne: null }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I want to search boolean values in my database. I am making a search API to search specific data from the data base. There are some invoices which are marked as dispatched and some invoices which payment has been received, I want to search those invoices with isDispatched and paymentPending boolean values. As you know $regex donot work with boolean values so what should I do to search boolean values in mongoDB data base.
Pretty late reply, but if you are trying to search based on the values stored perhaps try something like. { isDispatched: true, paymentPending: false} which will match documents where isDispatched is true and paymentPending is false.
Thank you. Actually these data are dynamic and if it is true or false that will be searched by the user. So I have to take input or for api, req.body or req.query for api endpoint. Api endpoint= '/search?query' or for req.body = /search and from req.body i will get these data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.