0

I need to find my data that matches the particular type. Say, I have an array of objects in my DB, with each object having it's leadType : all or leadType: blacklist. Each object has a different kind of leadType value associated with it.

I want to get the complete data that matches the particular leadType, like leadType: 'radar'.

I tried using the following query but along with the documents it matches, it also returns the empty documents that do not matches the specified projection.

This is what I tried:

exports.leads_by_type = (req, res) => {
  const { lead_type } = req.body;
  Lead.find({}, { leads: { $elemMatch: { leadType: lead_type }}} )
    .then(data => {
      if (!data || data.length === 0) {
        return res.status(404).json({ message: "No data found" });
      }
      res.status(200).json({ message: "Data found", data });
    })
    .catch(err => res.status(500).json({ err }));
};

and it returns me the output as follows:

{
  "message": "Data found",
  "data": [
      {
          "_id": "5e83427079f7611bdc1e48a0",
          "leads": [
              {
                  "education": {
                      "school": "happy kids corner",
                      "graduation": "some school"
                  },
                  "currentPosition": {
                      "title": "Salesforce developer",
                      "endDate": "currently working",
                      "employmentType": "full-time",
                      "industry": "information technology"
                  },
                  "location": {
                      "state": "delhi",
                      "country": "india"
                  },
                  "leadType": "radar",
                  "name": "Ben",
                  "mobile": 1524524678,
                  "_id": "5e83427079f7611bdc1e489e"
              }
          ]
      },
      {
          "_id": "5e84cb4fb59fdd1644e7c226",
          "leads": [
              {
                  "education": {
                      "school": "happy kids corner",
                      "graduation": "some school"
                  },
                  "currentPosition": {
                      "title": "Salesforce developer",
                      "endDate": "currently working",
                      "employmentType": "full-time",
                      "industry": "information technology"
                  },
                  "location": {
                      "state": "delhi",
                      "country": "india"
                  },
                  "leadType": "radar",
                  "name": "joey",
                  "mobile": 1524524678,
                  "_id": "5e84cb4fb59fdd1644e7c224"
              }
          ]
      },
      {
          "_id": "5e84cb70b59fdd1644e7c229",
          "leads": []
      },
      {
          "_id": "5e84cb88b59fdd1644e7c22c",
          "leads": []
      },
      {
          "_id": "5e84cbb7b59fdd1644e7c232",
          "leads": []
      },
      {
          "_id": "5e84cbd9b59fdd1644e7c235",
          "leads": [
              {
                  "education": {
                      "school": "happy kids corner",
                      "graduation": "some school"
                  },
                  "currentPosition": {
                      "title": "Salesforce developer",
                      "endDate": "currently working",
                      "employmentType": "full-time",
                      "industry": "information technology"
                  },
                  "location": {
                      "state": "delhi",
                      "country": "india"
                  },
                  "leadType": "radar",
                  "name": "rhea",
                  "mobile": 1524524678,
                  "_id": "5e84cbd9b59fdd1644e7c234"
              }
          ]
      }
  ]
}

Please help me out to fix remove these empty objects from the data output

Edit: Adding my DB screenshot: enter image description here

2 Answers 2

1

If you want to match only the documents with leadType equals to radar you should specify this in the find condition.

 exports.leads_by_type = (req, res) => {
  const { lead_type } = req.body;
  Lead.find({ 'leads.leadType': lead_type })
    .then(data => {
      if (!data || data.length === 0) {
        return res.status(404).json({ message: "No data found" });
      }
      res.status(200).json({ message: "Data found", data });
    })
    .catch(err => res.status(500).json({ err }));
};

all leads in my collection

with the condition

Sign up to request clarification or add additional context in comments.

4 Comments

I tried this too, but then it returns me the complete data with every leadType available
The query I wrote will return all documents that have any element in the array of leads with leadType equal to radar. If you want to return the array with the elements containing leadType radar only, then please tell me
Yes, I want to return documents containing the leadType equal to 'radar' only. The query I originally wrote, gives me the documents with type radar. and also, where the type is not equal to radar, it returns an empty array. I want to eliminate that.
I have edited my question with my existing db. Can you please check with that?
1

find({ leads: { $elemMatch: { leadType: lead_type }}} )

You can try the above query.

{} you had used empty object along with your condition in find which is the root cause behind the fetched empty data.

6 Comments

Still the same result. It returns me the complete data with every leadType available
@bubble-cord, mongoplayground.net/p/5Pw--9W9akB - This one is with the query I mentioned above mongoplayground.net/p/YiMPGQh4Vsa - This one is the query you had used You can find the difference in result.
Can you please create a chat for this? I am still unable to rectify my issue?
I have edited my question with my existing db. Can you please check with that?
Can you just copy above mentioned find portion and replace with the find portion in your query and check the output?
|

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.