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?