Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

107
Views
MongoDB find join array

I have a collection with data that looks sort of like this

{
  "part": [
    { "a": "1", "b": "a" },
    { "a": "23", "b": "b" },
    { "a": "4", "b": "c" },
  ]
}

What I would like is a way of searching for documents where the join of all a parts equals the search that I am looking for.

for example 1234 should match the document above, but 124 should not.

is this possible with MongoDB?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can do it with Aggregation framework:

  • $match with $eq - To filter only documents where concatenated a properties of part array are equal to the input string.
  • $reduce with $concat - To concatenate all a properties of part array for each document.
db.collection.aggregate([
  {
    "$match": {
      "$expr": {
        "$eq": [
          "1234",
          {
            "$reduce": {
              "input": "$part",
              "initialValue": "",
              "in": {
                "$concat": [
                  "$$value",
                  "$$this.a"
                ]
              }
            }
          }
        ]
      }
    }
  }
])

Working example

over 4 years ago · Santiago Trujillo Report

0

You can use aggregate with $reduce to join string then $match to filter your string.
Here is the playground.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!