Charts fails to detect coordinates, when following the docs.. Where is the glitch?

I have an issue that chats is not detecting the type “corodinates/location”. I always get back an array of Objects. I am following the docs to specify a geoPoint.

location: {
      type: "Point",
      coordinates: [-73.856077, 40.848447]
}

Here is the actual code:

},{
    $project: {
        _id: 0,  
        location: {
            type: "Point",
            coordinates: ['$longitude', '$latitude']
        },
        country: { $arrayElemAt: [ '$location.country_name', 0 ] } ,
        region: { $arrayElemAt: [ '$location.subdivision_1_name', 0 ] } ,
        device: changeEvent.fullDocument.sn
    }
  },{ 
    $merge: { 
      into: {
        "db": "myDB",
        "coll": "myColl"
      }, 
      on: [ "myKey"],
      whenMatched: "merge",
      whenNotMatched: "discard"
    } 
  }

The resulting structure looks like this (mind the Array after ‘location’!)
image

For some reason location becomes an array and this seems to fool chats, so that the coordinates are not detected as coordinates/location but as an array:
image

It should look like this
image
(Example taken from the airbnb sample data)

How can I get the coordinates detected in charts so that I can make use of them??

Regards,
Michael

Hi Michael -

Your location data is in GeoJSON format, which is something that Charts is able to deal with directly. However unfortunately we can’t handle arrays of GeoJSON objects, which is what you have. The best solution would be to use $unwind in the query bar to get rid of the array, and then it should detect the location data correctly.

Tom

2 Likes

Note that if "location" is an array in the original document (as I suspect it is because you’re using $arrayElemAt to get a single country name from it) then the output will also make it an array.

Can you provide the original document format in source collection as well as document format in myColl that you are outputting to? Both projection and updates will traverse arrays and preserve their “array-ness”…

Asya

3 Likes

Hello @Asya_Kamsky

thanks a lot, next time we meet the dink is on me…

This is a stupid mistake I made. Before I $project I have a $lookup stage where I use as: 'location' this is an array. For whatever reason I used location again in the $project to name the projected Object. So in $project the array is picked up from the $lookup and the aggregation does what it should do.
Simply changing the name of the field fixes my mistake and the $project returns an object instead of an array.

Guess this is a further entry on the “don’t do this” list…

The new output:

"location_new_field_name": {
      "type": "Point",
      "coordinates": [
        22.9414,
        39.3616
      ]
    },
    "country": "Greece",
    "region": "Thessaly",
    "devicemac": "01:02:03:04:05:06"
  }

The (incorrect) aggregation → in $project the objectname should NOT be location since this conflicts with the location in the $lookup:

,{
    $lookup: {
      from: 'geoip_location',
      localField: 'geoname_id',
      foreignField: 'geoname_id',
      as: 'location'
    }
  },{
    $project: {
        _id: 0,  
        location: {
            type: "Point",
            coordinates: ['$longitude', '$latitude']
        },
        country: { $arrayElemAt: [ '$location.country_name', 0 ] } ,
        region: { $arrayElemAt: [ '$location.subdivision_1_name', 0 ] } ,
        devicemac: changeEvent.fullDocument.sn
    }
  }

Cheers, Michael

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.