Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import json file into collection in server code on startup

I've exported a MongodB collection to a JSON file on my local test machine and want to import it through the Meteor.js server side code on startup (after deploying to a meteor.com site). I'm not finding any examples of this yet.

Thanks

like image 487
James Avatar asked Dec 20 '22 12:12

James


1 Answers

Example:

    // import data only when Products collection is empty

    if (Products.find().count() === 0) {
        console.log("Importing private/products.json to db")

        var data = JSON.parse(Assets.getText("products.json"));

        data.forEach(function (item, index, array) {
            Products.insert(item);
        })
    }
like image 91
Kuba Wyrobek Avatar answered Dec 24 '22 01:12

Kuba Wyrobek