Skip to content

Instantly share code, notes, and snippets.

@aaronbassett
Last active March 22, 2020 23:35
Show Gist options
  • Save aaronbassett/17b0284b40960d51a6e550da10001232 to your computer and use it in GitHub Desktop.
Save aaronbassett/17b0284b40960d51a6e550da10001232 to your computer and use it in GitHub Desktop.
dev.to - Finding users within X km/miles of a zip code
{
"_id":{
"$oid":"5e4182c55b13920bcc41ff64"
},
"address":{
"address1":"Suite 442",
"address2":"855 Owens Track",
"city":"Burwell",
"country":"USA",
"state":"NE",
"zip":"68823",
"location":{
"type":"Point",
"coordinates":[-99, 41.9]
}
},
"name":"Julia Carroll"
}
{
"_id":{
"$oid":"5e4182c55b13920bcc41ff64"
},
"address":{
"address1":"Suite 442",
"address2":"855 Owens Track",
"city":"Burwell",
"country":"USA",
"state":"NE",
"zip":"68823"
},
"name":"Julia Carroll"
}
from uszipcode import SearchEngine
zip_search = SearchEngine(simple_zipcode=True)
zipcode = zip_search.by_zipcode('90210')
zipcode = zipcode.to_dict()
customers = db.users.aggregate(
[
{
"$match": {
"address.location": {
"$geoWithin": {
"$centerSphere": [
[zipcode["lng"], zipcode["lat"]],
25 / 3963.2,
]
}
},
},
},
],
)
from uszipcode import SearchEngine
zip_search = SearchEngine(simple_zipcode=True)
zipcode = zip_search.by_zipcode(user['zipcode'])
zipcode = zipcode.to_dict()
db.users.update_one(
{"_id": user['id']},
{"$set": {
"location.type": "Point",
"location.coordinates": [zipcode['lng'], zipcode['lat']]
}}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment