Last active
February 11, 2021 18:49
-
-
Save JoeKarlsson/0e2ce20c41860db2acf0e2fb2381cb42 to your computer and use it in GitHub Desktop.
Script written in Node.js for seeding IoT Data to a MongoDB Atlas database.
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
/* mySeedScript.js */ | |
// require the necessary libraries | |
const faker = require("faker"); | |
const MongoClient = require("mongodb").MongoClient; | |
function randomIntFromInterval(min, max) { // min and max included | |
return Math.floor(Math.random() * (max - min + 1) + min); | |
} | |
async function init() { | |
// Connection URL | |
const uri = "YOUR MONGODB ATLAS URI"; | |
const client = new MongoClient(uri, { | |
useNewUrlParser: true, | |
// useUnifiedTopology: true, | |
}); | |
try { | |
await client.connect(); | |
console.log("Connected correctly to server"); | |
const collection = client.db("iot").collection("kitty-litter-time-series"); | |
// The drop() command destroys all data from a collection. | |
// Make sure you run it against proper database and collection. | |
collection.drop(); | |
// make a bunch of time series data | |
let timeSeriesData = []; | |
for (let i = 0; i < 5000; i++) { | |
const firstName = faker.name.firstName(); | |
const lastName = faker.name.lastName(); | |
let newDay = { | |
timestamp_day: faker.date.past(), | |
cat: faker.random.word(), | |
owner: { | |
email: faker.internet.email(firstName, lastName), | |
firstName, | |
lastName, | |
}, | |
events: [], | |
}; | |
for (let j = 0; j < randomIntFromInterval(1, 6); j++) { | |
let newEvent = { | |
timestamp_event: faker.date.past(), | |
weight: randomIntFromInterval(14,16), | |
} | |
newDay.events.push(newEvent); | |
} | |
timeSeriesData.push(newDay); | |
} | |
collection.insertMany(timeSeriesData); | |
console.log("Database seeded! :)"); | |
client.close(); | |
} catch (err) { | |
console.log(err.stack); | |
} | |
} | |
init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment