Answer the question
In order to leave comments, you need to log in
How to create a query to MongoDB to filter by several fields at once?
I have a student object
[
{
"_id": "60f18b0120e927a4bf5a3654",
"balance": 0,
"groups": [
"id1",
"id2",
"id3"
],
"name": "Хабиб",
"surname": "Нурмагомедов",
"midname": "Абдулманапович",
"gender": "Мужской",
"age": "2005-05-06T00:00:00.000Z"
}
]
balance
- Negative, positive, or both gender
- Male, Female, or both age
- There should be an array $or
of conditions, let's say , we are looking for people aged 13, 15 and 19 years old. Age is stored as a date of birth in ISO format, and the algorithm for finding people of the right age works like this: age
must be $gte: /*какая-то дата*/
and $lt: /*какая-то дата*/
(Operators >, <, =, etc. are applicable to the date) groups
- the student in the example has only three groups - id1, id2, id3, you need to filter out students who have groups in the groups field from the id array, which we get as a filter. {
groups: ["id1", "id3", "id8"]
},
{
groups: ["id1", "id2", "id5"]
},
{
groups: ["id6", "id7", "id8"]
}
["id1", "id2"]
{
groups: ["id1", "id3", "id8"]
},
{
groups: ["id1", "id2", "id5"]
}
$all
, but I would at least have to combine the first, second and third filters for now ... Answer the question
In order to leave comments, you need to log in
Ok, I found a solution - MongoDB Compass has an aggregate pipeline constructor
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question