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

400
Views
mongodb - concat array field from many documents to one array field

I have this query:

 [
  {
    '$match': {
      'imageUrls': {
        '$type': 'array', 
        '$ne': []
      }
    }
  }, {
    '$project': {
      'imageUrls': 1, 
      'time': 1
    }
  }, {
    '$sort': {
      'time': 1
    }
  }, {
    '$skip': 0
  }, {
    '$limit': 10
  }
]

This is the schema:

{
    "_id": {
        "$oid": "5ffb0c14dd482daa039ee906"
    },
    "imageUrls": ["image url1", "image url2"],
    "time": 342432432432
}

The output is a list of documents, each containing the array field imageUrls. I want to have all the values in the imageUrls fields in one single array field.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

  • $sort by time in descending order
  • $project to show only one field imageUrls
  • $unwind deconstruct imageUrls array
  • $facet to separate both result, total and pagination documents
  • $skip documents
  • $limit number of documents
  • $group by null and reconstruct imageUrls array
  { $sort: { time: -1 } },
  { $project: { imageUrls: 1 } },
  { $unwind: "$imageUrls" },
  {
    $facet: {
      result: [
        { $skip: 0 },
        { $limit: 10 },
        {
          $group: {
            _id: null,
            imageUrls: { $push: "$imageUrls" }
          }
        }
      ],
      count: [
        { $count: "count" }
      ]
    }
  }

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!