For testing purposes, I created a collection with only one document:
{
"_id" : ObjectId("5ee7ba7ea3ac0c3192c19e58"),
"workspace_id" : 1,
"attributes" : {
"first_name" : "John",
"last_name" : "Doe",
"email" : "john.doe@example.net",
"phone_number" : "+1234567890",
"gender" : "Male"
},
"events" : [
{
"event" : "search_results",
"event_data" : {
"query" : "Shirts",
"results" : [
{
"name" : "Red T-Shirt XL"
}
]
}
},
{
"event" : "search_results",
"event_data" : {
"query" : "Shirts",
"results" : [
{
"name" : "Blue T-Shirt XL"
}
]
}
}
],
"created_at" : ISODate("2020-03-17T09:58:02.000Z"),
"updated_at" : ISODate("2020-03-17T09:58:02.000Z")
}
Now, I want to query all clients who had “search_results” but whose results are those where Red T-Shirt XL and Blue T-Shirt XL appeared.
This is my query:
db.getCollection('clients').find(
{
"$and": [
{
"workspace_id": 1,
"deleted_at": {
"$exists": false
}
},
{
"events.event": "search_results",
"$and": [
{"events.event_data.results.name" : "Red T-Shirt XL"},
{"events.event_data.results.name" : "Blue T-Shirt XL"},
]
}
]
}
)
However, it returns this ONE record, but shouldn’t… It should if Red and Blue shirts are in results array. Like this:
{
"event" : "search_results",
"event_data" : {
"query" : "Shirts",
"results" : [
{
"name" : "Red T-Shirt XL"
},
{
"name" : "Blue T-Shirt XL"
}
]
}
}
Thank you!