1

I have a document like this:

{
        "_id": "5ffc130e9fb31b26162e0bad",
        "results": [
            {
                "customer": {
                    "display_name": "Manno Dispensary - first",
                    "ext_acct_id": "267"
                }
            },
            {
                "customer": {
                    "display_name": "Manno Dispensary - second",
                    "ext_acct_id": "262"
                }
            },
            {
                "customer": {
                    "display_name": "Kako Dispensary - first",
                    "ext_acct_id": "261"
                }
            },
            {
                "customer": {
                    "display_name": "Kako Dispensary - second",
                    "ext_acct_id": "263"
                }
            }
        ]
}

I want to write a MongoDB query which does a regex search on "customer.display_name" and returns all those documents in results that satisfies this criteria.

I have written this query till now, and it returns me the desired output, but the problem is, it is only retuning one document inside results, Am I missing anything in this?

my desired output:

{
            "_id": "5ffc130e9fb31b26162e0bad",
            "results": [
                {
                    "customer": {
                        "display_name": "Manno Dispensary - first",
                        "ext_acct_id": "267"
                    }
                },
                {
                    "customer": {
                        "display_name": "Manno Dispensary - second",
                        "ext_acct_id": "262"
                    }
                }
            ]
    }

What I am actually getting:

{
                "_id": "5ffc130e9fb31b26162e0bad",
                "results": [
                    {
                        "customer": {
                            "display_name": "Manno Dispensary - first",
                            "ext_acct_id": "267"
                        }
                    }
                ]
        }

this is the query which I have written to fetch all customers which contain "Manno" in their customer name.

My collection name is Order(mongoose), search="Manno"

Order.find({
      results: {
        $elemMatch : {
          "customer.display_name": {$regex: search}
        }
      }
    },{
      results: {
        $elemMatch : {
          "customer.display_name": {$regex: search}
        }
      } 
    });
0

1 Answer 1

0

The $elemMatch and $ will return only single matching document/object form array, try $filter and $regexMatch,

  • $filter to iterate loop of results array
  • $regexMatch to check regular expression condition, it will return true or false
Order.find({
  results: {
    $elemMatch: {
      "customer.display_name": { $regex: "Manno" }
    }
  }
},
{
  results: {
    $filter: {
      input: "$results",
      cond: {
        $regexMatch: {
          input: "$$this.customer.display_name",
          regex: "Manno"
        }
      }
    }
  }
})

Playground

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

1 Comment

didn't work for me. finally, I changed the database schema and made a new query

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.