Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
352 views
in Technique[技术] by (71.8m points)

regex - How to project all nested documents from an array of objects?

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}
        }
      } 
    });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...