This structure is complex for me because of “children”. I can not write aggregate query. Here is my collection:
{
"_id" : ObjectId("6213ba90a013b7c5f1232e1f"),
"birthDate" : ISODate("1965-01-04T23:30:15Z"),
"surname" : "White",
"name" : "Adam",
"registerDate" : ISODate("2018-04-13T08:50:30Z"),
"children" : [
{
"surname" : "White",
"name" : "Belly",
"birthDate" : ISODate("1985-01-21T09:30:15Z")
},
{
"surname" : "White",
"name" : "Michael",
"birthDate" : ISODate("1987-02-17T11:30:15Z")
}
],
"city" : "Chicago"
}
{
"_id" : ObjectId("6213ba9fa013b7c5f1232e21"),
"birthDate" : ISODate("1969-03-01T13:30:15Z"),
"surname" : "Dash",
"name" : "Levon",
"registerDate" : ISODate("2018-04-06T15:30:15Z"),
"children" : [
{
"birthDate" : ISODate("1989-03-21T15:30:15Z"),
"surname" : "Dash",
"name" : "Alexander"
},
{
"birthDate" : ISODate("1991-04-21T17:30:15Z"),
"surname" : "Dash",
"name" : "Katy",
"registerDate" : ISODate("2019-08-18T13:30:15Z")
}
],
"city" : "Chicago"
}
{
"_id" : ObjectId("6213baa5a013b7c5f1232e23"),
"birthDate" : ISODate("1973-05-01T13:30:15Z"),
"surname" : "Johnson",
"name" : "Emma",
"registerDate" : ISODate("2018-06-11T17:30:15Z"),
"children" : [
{
"birthDate" : ISODate("1993-05-21T16:30:15Z"),
"surname" : "Johnson",
"name" : "Liam"
},
{
"birthDate" : ISODate("1994-01-21T15:30:15Z"),
"surname" : "Johnson",
"name" : "Olivia",
"registerDate" : ISODate("2019-09-14T12:30:15Z")
}
],
"city" : "Houston"
}
I want to write aggragate query that calculates average registration age of members that registered to our system based on their cities. How can I write that query?