H
H
hokuzz2021-08-05 20:24:39
Mongoose
hokuzz, 2021-08-05 20:24:39

How to implement the right query to find what you need?

Hello, I started writing some kind of system. In general, there is an array that is in the database, and you need to find exactly the schema where the user with a certain identifier is located.

{
  objID: 1,
  [...]
  members: [
    { author: 101, rank: "какой либо ранг (он может быть рандомный)" },
    [...]
  ]
}


How to correctly implement the request so that it checks if the schema contains the person I need with the specified ID?

<>.findOne({ objID: 1, members: { $in: { author: 101 } } });


I tried to do it in this way, but .. It finds only if I specify the rank along with the id . Is it possible to implement it in such a way that it searches only by id, without indicating the rank? Can rebuild the data array in some way to make it more convenient? PS: You can not suggest that you first receive the data and then find it through some kind of [].find() , I need it right away with a request.


Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stockholm Syndrome, 2021-08-05
@hokuzz

most likely you need $elemMatch

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question