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

124
Views
can't find in array that references another collection mongoose

I have this data whose 'contacts' field references another collection:

{
   "_id":{"$oid":"61549125765ffb0f0477678d"},
   "contacts":[{"$oid":"61549123765ffb0f04776787"}],
   "username":"petete",
   "phoneNumber":"+222222222222",
   "__v":{"$numberInt":"0"},
   "socketId":"CEvkxvonNfXyhZMFAAAJ"
}

If i find it with populate(), i get this:

 {
      contacts: [  //i want to show this array
        {
          contacts: [Array],
          _id: 61549123765ffb0f04776787,
          username: 'tachancka',
          phoneNumber: '+111111111111',
          __v: 0,
          socketId: 'C3srKMQB80tIrC2jAAAK'
        }
      ],
      _id: 61549125765ffb0f0477678d,
      username: 'petete',
      phoneNumber: '+222222222222',
      __v: 0,
      socketId: 'CEvkxvonNfXyhZMFAAAJ'
 }

So i need to find the first 'contacts' array by id. I tried with this code, but it returns null:

User.findOne({_id: userLoggedId, 'contacts._id': contactId}).populate({
   path: 'contacts',
   model: 'User'
})

And these are my consts:

userLoggedId = '61549125765ffb0f0477678d'
contactId = '61549123765ffb0f04776787'

What am i doing wrong?

about 4 years ago · Juan Pablo Isaza
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!