I need to perform a MongoDB aggregation in PHP.
I read about the MongoCollection::aggregate method but to use the MongoCollection class I need to go through the MongoClient class, which is deprecated.
In the docs it is said to use the MongoDB driver instead and the MongoDB\Driver\Manager class. I already use this class for all my queries, updates and inserts, but I can't find a way to execute aggregations.
This topic does not help me because it uses regular queries.
I am quite surprised I can't find any answers out there.
Maybe I am focusing on aggregations while I don't need them so here is what I am trying to do:
I have a users
collection that contains a peels
field which is an array of string.
I would like to count for a given user the number of elements of its peels
field matching a specific regex.
For instance, if I have this user:
{
"_id": ObjectId("5ff337f8415b0000f9003b78"),
"name": "Foo",
"peels": [
"2021-03-08",
"2021-04-19",
"2020-07-20",
"2020-08-10"
]
}
I want to get the number of dates of the user for the year 2021, so matching the regex 2021-*
.
This should give me 2
.
If this can indeed be solved without aggregations it would still be interesting to know how to use aggregation with the MongoDB/Driver/Manager or any other solution that is not deprecated.