Hackathon processing (sharing a tip on geo codes)

OK, so I have the data from downloading but I need to get the geocoding compatible. This works well for me:

[{$addFields: {
 actorCodes: {
  $concatArrays: [
   [
    '$Actor1Name'
   ],
   [
    '$Actor2Name'
   ]
  ]
 },
 geoCodes: {
  $concatArrays: [
   [
    [
     '$ActionGeo_Long',
     '$ActionGeo_Lat'
    ]
   ],
   [
    [
     '$Actor1Geo_Long',
     '$Actor1Geo_Lat'
    ]
   ],
   [
    [
     '$Actor2Geo_Long',
     '$Actor2Geo_Lat'
    ]
   ]
  ]
 }
}}, {$addFields: {
 geoCodes: {
  $map: {
   input: '$geoCodes',
   as: 'a',
   'in': {
    $map: {
     input: '$$a',
     as: 'b',
     'in': {
      $convert: {
       input: '$$b',
       to: 'double',
       onError: ''
      }
     }
    }
   }
  }
 }
}}, {$addFields: {
 geoCodes: {
  $filter: {
   input: '$geoCodes',
   as: 'b',
   cond: {
    $and: [
     {
      $ne: [
       {
        $arrayElemAt: [
         '$$b',
         0
        ]
       },
       ''
      ]
     },
     {
      $ne: [
       {
        $arrayElemAt: [
         '$$b',
         1
        ]
       },
       ''
      ]
     }
    ]
   }
  }
 }
}}, {$addFields: {
 points: {
  $map: {
   input: '$geoCodes',
   'in': {
    geometry: {
     type: 'Point',
     coordinates: '$$this'
    }
   }
  }
 }
}}, {$project: {
 geoCodes: 0
}}, {$merge: {
 into: 'eventscsv',
 on: '_id',
 whenMatched: 'replace',
 whenNotMatched: 'discard'
}}]
3 Likes

Great tip, @Ilan_Toren ! Thanks so much for sharing!

Indeed - thanks for sharing with the community @Ilan_Toren

Thanks for sharing this @Ilan_Toren . I was trying to use it but I got an error:

However after reading up and removing the empty stage object without a field it worked nicely

image

Thanks again for sharing this. Made me properly read about aggregations in mongoDB for the first time.

1 Like