I have two separate aggregations queries that work well.
First one pulls all the unique addresses (field name is “location”).
Second one counts how many times a street name is repeated. For example it prints out if John Street is used 1 time or 10 times. This is NOT REPEATS. This is just because there are many houses on the same street. “location” field content examples are:
1 John street
2 John street
5 John street
1 Peter street
8 Peter street
100 Yoyo street
How can I combine the below two aggregations so results of first aggregation is used by second aggregation and final result is printed?
1st aggregation:
[
{
$group: {
_id: “$location”,
count: { $sum: 1 },
},
},
{ $match: { count: { $gt: 0 } } },
{
$group: {
_id: null,
totalCount: { $sum: 1 },
content: { $push: “$$ROOT._id” },
},
},
{ $project: { _id: 0 } },
]
Second aggregation:
[
{
$addFields: {
parsedAddress: {
$arrayElemAt: [
{
$getField: {
field: “captures”,
input: {
$regexFind: {
input: “$location”,
regex: /\d+\s(.+)/,
},
},
},
},
0,
],
},
},
},
{
$group: {
_id: “$parsedAddress”,
count: {
$sum: 1,
},
},
},
]
Thanks