Hi everyone,
Is it possible to use elemMatch within filter?
This is an example of my data:
{
"_id" : ObjectId("5e6c26153facb910290f0869"),
"attributes" : [
{
"k" : "first_name",
"v" : "John"
},
{
"k" : "last_name",
"v" : "Doe"
},
{
"k" : "email",
"v" : "john.doe@example.net"
},
],
"events" : [
{
"event" : "add_to_cart",
"event_data" : [
{
"k" : "product_name",
"v" : "T-shirt"
},
{
"k" : "price",
"v" : 30
}
],
"created_at" : ISODate("2020-03-14T00:32:21.000Z")
},
],
"created_at" : ISODate("2020-03-14T00:32:21.000Z"),
}
I would like how it is possible to get users who had event “add_to_cart” with specific key/value attributes.
db.getCollection('clients').aggregate([
{
"$match": {
"deleted_at": {
"$exists": false
}
}
},
{
"$addFields": {
"event_count": {
'$size': {
"$filter" : {
"input" : "$events",
'as' : 'events',
'cond' : {
'$and': {
// How to add elemMatch here?**
}
}
}
}
}
}
},
])
How to combine $elemMatch and $filter?