My structure is as follows:
{
day: x,
events:
[
{
year: y,
info: z
}
]
}
Up to now I created the following query, which I does not return an error but does show anything either (which is wrong).
db.days.aggregate([
{
$match:
{
$and:
[
{
'day': 'March_13'
},
{
'events.year': '1870'
},
{
'events.info': {$regex: "./French./"}
}
]
}
},
{
$unwind: {path: "$events"},
},
{
$match:
{
'info': { $regex: '.*French.*'}
}
}])
From what I read I need to group by _id, but I do not know how to recreate the array with the objects that satisfied the second $match.
Could you please have a look and maybe tell me why the initial query does not work and advise me on a group block?
Some sample data is here:
{
"day" : "March_13",
"events" :
[
{
"year" : "1929",
"info" : "Peter Breck, American actor (d. 2012)"
},
{
"year" : "1929",
"info" : "Joseph Mascolo, American actor"
},
{
"year" : "1929",
"info" : "Zbigniew Messner, Polish economist and politician, 9th Prime Minister of the Republic of Poland (d. 2014)"
},
{
"year" : "1929",
"info" : "Bunny Yeager, American model and photographer (d. 2014)"
}
]
}
And here is if I would succeed in querying by the word "American":
{
"day" : "March_13",
"events" :
[
{
"year" : "1929",
"info" : "Peter Breck, American actor (d. 2012)"
},
{
"year" : "1929",
"info" : "Joseph Mascolo, American actor"
},
{
"year" : "1929",
"info" : "Bunny Yeager, American model and photographer (d. 2014)"
}
]
}
Basically I want to check if the field info contains the searched word, and if it does I keep it in the array.