Last active
March 22, 2020 23:35
-
-
Save aaronbassett/17b0284b40960d51a6e550da10001232 to your computer and use it in GitHub Desktop.
dev.to - Finding users within X km/miles of a zip code
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
{ | |
"_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" | |
} |
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
{ | |
"_id":{ | |
"$oid":"5e4182c55b13920bcc41ff64" | |
}, | |
"address":{ | |
"address1":"Suite 442", | |
"address2":"855 Owens Track", | |
"city":"Burwell", | |
"country":"USA", | |
"state":"NE", | |
"zip":"68823" | |
}, | |
"name":"Julia Carroll" | |
} |
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
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, | |
] | |
} | |
}, | |
}, | |
}, | |
], | |
) |
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
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