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

95
Views
How to find specific object from array in MongoDB

Suppose I have this MongoDB document:

{
  "_id": {
    "$oid": "628f739398580cae9c21b44f"
  },
  "place":"Amsterdam",
  "events": [
    {
      "eventName": "Event one",
      "eventText": "Event",
      "eventDate": "010101"
      "host":"Bob"
    },
    {
      "eventName": "E2",
      "eventText": "e2",
      "eventDate": "020202"
      "host":"John"
    }
  ]
}

{
  "_id": {
    "$oid": "628f739398580cae9c21b44f"
  },
  "place":"London",
  "events": [
    {
      "eventName": "e3",
      "eventText": "e3",
      "eventDate": "010101",
      "host":"Bob"
    }
  ]
}

And I want to get the array block of events being hosted by Bob, how would I do that? I have tried to use $elemSelector like this:

const result = await mongoDatabase
      .collection("temp")
      .find({ events: { $elemMatch: { eventName: "E2" } } })
      .toArray();

    console.log(JSON.stringify({ result }));

But it just returns all events at the same place as the selected, Amsterdam in this case.

Wanted result:

{
  "eventName": "Event one",
  "eventText": "Event",
  "eventDate": "010101"
  "host":"Bob"
},
{
  "eventName": "e3",
  "eventText": "e3",
  "eventDate": "010101",
  "host":"Bob"
}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Check this out

db.collection.aggregate({
  $unwind: "$events"
},
{
  $match: {
    "events.host": "Bob"
  }
},
{
  "$project": {
    "events": 1,
    "_id": 0
  }
},
{
  "$replaceRoot": {
    "newRoot": "$events"
  }
})

And you can play around with more test data here: https://mongoplayground.net/p/A1UbZdZgZmr

about 4 years ago · Juan Pablo Isaza Report

0

You can achieve with simple pipeline stages

db.collection.aggregate([
  {
    "$match": {//Match
      "events.host": "Bob"
    }
  },
  {
    "$project": {
      "e": {
        "$filter": {//Filter matching elements
          "input": "$events",
          "as": "event",
          "cond": {
            "$eq": [
              "$$event.host",
              "Bob"
            ]
          }
        }
      }
    }
  }
])

Playground

about 4 years ago · Juan Pablo Isaza Report

0

Following Structure can be achieved by using projection query in MongoDB one lucid solution for the above can be this

 db.collection("temp").find({ "events":{ $elemMatch:{ "eventName":"e3" }  } }, {"events":1,"_id":0})
about 4 years ago · Juan Pablo Isaza 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!