启动时将json文件导入服务器代码中的集合(Import json file into collection in server code on startup)
我已经将MongodB集合导出到本地测试机器上的JSON文件,并且希望在启动时(在部署到meteor.com网站之后)通过Meteor.js服务器端代码导入它。 我还没有找到任何这方面的例子。
谢谢
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
最满意答案
例:
// 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); }) }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); }) }启动时将json文件导入服务器代码中的集合(Import json file into collection in server code on startup)我已经将MongodB集合导出到本地测试机器上的JSON文件,并且希望在启动时(在部署到meteor.com网站之后)通过Meteor.js服务器端代码导入它。 我还没有找到任何这方面的例子。
谢谢
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
最满意答案
例:
// 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); }) }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); }) }
发布评论