A
A
Andrew2020-09-13 12:54:21
MongoDB
Andrew, 2020-09-13 12:54:21

How to select all chats a user is a member of?

Tell me, please, what am I doing wrong? already tried everything. I have a CHAT object. 5f5deb0da6536155727140.png
I'm trying to find all the chats that a particular user is a member of. those. I am looking for all chats that have an object in the chatMembers array whose property username = I need, but for some reason such a request does not give a result:

const chats = await Chat.find({ chatMembers: { $in: [{ username: req.username /* = "usver" */ }] } })

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hzzzzl, 2020-09-13
@JustAndew

most likely so, because it is chatMembers.username that needs to be checked for entry into the array

const chats = await Chat.find({ 'chatMembers.username': { $in: ['usver', 'another_user'] } })

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question