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

668
Views
MongoDB: How to find specific value in nested array, if found then show only that array not all?

enter image description here

I am trying to find id "D0182" in client_id array if found then only it will show the client_id document, but it is showing me the whole document.

Here is my query -

db.getCollection('news').find({"newsSources.client_id":"A0003","date": "2021-01-22"}, {"_id":0,"newsSources.title" :1, "newsSources.client_id":1})

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can use aggregation to filter out objects from array.

db.collection.aggregate([
  {
    "$addFields": {
      "newsSources": {
        $filter: {
          input: "$newsSources",
          cond: {
            $in: [
              "D0108",
              "$$this.clientId"
            ]
          }
        }
      }
    }
  }
])

Working Mongo 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!