Created
August 7, 2019 14:48
-
-
Save lrlna/d1779ea4ece061cfcffef085bbd53978 to your computer and use it in GitHub Desktop.
Populate MongoDB with some test data
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
// To run this you need: | |
// 1. To get an API key from google: https://developers.google.com/maps/documentation/javascript/get-api-key | |
// 2. Copy that key and add it to a file in the same directory you have this script in, ./keys.json will be read out to make a call to google maps. | |
var MongoDBClient = require('mongodb').MongoClient | |
var googleMaps = require('@google/maps') | |
var path = require('path') | |
var fs = require('fs') | |
var keys = fs.readFileSync(path.join(__dirname, './keys.json')) | |
var key = JSON.parse(keys).google_key | |
var mapsClient = googleMaps.createClient({ key }) | |
var location = [52.5200, 13.4050] | |
var query = "bar" | |
mapsClient.places({ query: query, location: location }, function (err, res) { | |
if (err) console.log('GOOGLE MAPS ERROR', err) | |
console.log(res.json.results) | |
insertIntoDB(res.json.results) | |
}) | |
function insertIntoDB (mapsResults) { | |
var url = 'mongodb://localhost:27017' | |
var dbName = 'bars' | |
var mongodbClient = MongoDBClient(url) | |
mongodbClient.connect(function (err, client) { | |
if (err) console.log('MONGODB ERR', err) | |
var db = client.db(dbName) | |
db.collection('berlin').insertMany(mapsResults, function (error, result) { | |
if (error) console.log('INSERT MANY ERR', error) | |
console.log('INSERTED COUNT', result.insertedCount) | |
client.close() | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment