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

112
Views
find element in array within array mongoose

I have this data:

[
  {
    contacts: [ [Object] ],
    _id: 614a1301a926d73628d791fe,
    username: 'erick',
    socketId: 'OB8uqmHnsGPKC3gcAAAB'
  },
  {
    contacts: [ 
       contacts: [ [Object] ],
       _id: 614a1301a926d73628d791fe,
       username: 'erick',
       socketId: 'OB8uqmHnsGPKC3gcAAAB'
    ],
    _id: 614a1306a926d73628d79204,
    username: 'tachancka',
    socketId: 'cN333_zn8r48K_0tAAAB'
  }
]

And i need to find the username of 'erick' inside 'tachancka' contacts. I have tried a few ways to do it but none of them worked for me, returning null.

For example:

User.findOne({contacts: {$elemMatch:{'username': 'erick'}}}) 

Or:

User.findOne({'contacts.username': 'erick'})
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

pls refer to sample here: https://mongoplayground.net/p/Np_MdrubPgT

you may use

db.User.find({
  username: "tachancka",
  "contacts.username": "erick"
})

When you have array of objects and if you want to match all the field within object then we use $elemMatch, here in your case i feel username is not part of contacts array hence you will not use $elemMatch

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!