Last active
October 4, 2021 14:27
-
-
Save ilantoren/220cf6c55f8baa162a003fa28ac1a382 to your computer and use it in GitHub Desktop.
Revised Realm function for integration into Flutter application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* restaurantsByNeighborhood | |
* @param string neighborhood | |
* @returns object | |
*/ | |
exports = function(neighborhood){ | |
// A simple function to create an average coordinate | |
// for centering the map. | |
const avg = function( arr ) { | |
let k = 0; | |
let l = 1 | |
for( let j of arr) { | |
k=k+j | |
l=l+1 | |
} | |
return k/l | |
} | |
// result is what is passed back | |
let result = {} | |
// aggregation pipeline for the neighborhood collection | |
// this was taken from the Compass export to code menu for javascript | |
let pipeline = [{$match: { | |
name: neighborhood | |
}}, {$project: { | |
name:1, | |
coord: {$arrayElemAt:["$geometry.coordinates", 0]}, | |
_id: 0 | |
}}, {$addFields: { | |
long: {$map: {input: "$coord", in: {$arrayElemAt: ["$$this", 0]}}}, | |
lat: {$map: {input: "$coord", in: {$arrayElemAt: ["$$this", 1]}}} | |
}}]; | |
// atlas specific - getting a db connection to the two collections: neighborhoods and restaurants | |
let collection = context.services.get("mongodb-atlas").db("sample_restaurants").collection("neighborhoods"); | |
let restaurants = context.services.get("mongodb-atlas").db("sample_restaurants").collection("restaurants"); | |
class latlng { | |
constructor( lat, lng ) { | |
this.lat =lat; | |
this.lng = lng; | |
} | |
} | |
collection.aggregate( pipeline ).next().then( a => { | |
if (a ) { | |
// dart deserialization has a quirk with double[][] fields. | |
//One solution is to use js to map the data to a latlng type object | |
let mycoords = a.coord.map( (a) => new latlng( a[1], a[0])); | |
result.coord = mycoords; | |
let long = a.long; | |
result.avg_long = avg( long ); | |
let lat = a.lat; | |
result.avg_lat = avg( lat ); | |
result.name = a.name; | |
const resultList = restaurants.find( {"address.coord" : {$geoWithin: { $geometry: {type: "Polygon", coordinates: [a.coord] }}}}).toArray(); | |
result.restaurants = resultList; | |
result.long = long; | |
result.lat = lat; | |
} | |
}); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment