在流星或节点中批量插入mongodb [英] Bulk mongodb insert in Meteor or Node
问题描述
MongoDB支持批量插入 http://docs.mongodb.org/manual/core /bulk-inserts/
MongoDB supports bulk insert http://docs.mongodb.org/manual/core/bulk-inserts/
我已经在Meteor集合中尝试过:
I have tried it in Meteor collection:
Orders.insert([
{ "cust_id" : "A123", "amount" : 500, "status" : "A", "_id" : "iZXL7ewBDALpic8Fj" },
{ "cust_id" : "A123", "amount" : 250, "status" : "A", "_id" : "zNrdBAxxeNZQ2yrL9" },
{ "cust_id" : "B212", "amount" : 200, "status" : "A", "_id" : "vev3pjJ8cmiDHHxe4" },
{ "cust_id" : "A123", "amount" : 300, "status" : "D", "_id" : "BBLngRhS76DgeHJQJ" }
]);
但是它只是创建
{ "0" : { "cust_id" : "A123", "amount" : 500, "status" : "A", "_id" : "iZXL7ewBDALpic8Fj"},
"1" : { "cust_id" : "A123", "amount" : 250, "status" : "A", "_id" : "zNrdBAxxeNZQ2yrL9" },
"2" : { "cust_id" : "B212", "amount" : 200, "status" : "A", "_id" : "vev3pjJ8cmiDHHxe4" },
"3" : { "cust_id" : "A123", "amount" : 300, "status" : "D", "_id" : "BBLngRhS76DgeHJQJ" },
"_id" : "6zWayeGtQCdfS65Tz" }
出于性能测试目的,我需要它.我需要用数千个测试项目填充和测试数据库.我确实在foreach中插入了内容,但填充数据库所需的时间太长.
I need it for performance testing purposes. I need to fill and test database with thousands of testing items. I do inserts in foreach, but it takes too long to fill database.
这里有什么解决方法吗?还是我们可以预期Meteor在下一个版本中将支持此功能?
Is here any workaround? Or can we expect Meteor will support this in next versions?
推荐答案
您可以使用exec( nodejs docs )在服务器上的Meteor.startup
内部的流星内部运行mongo脚本.
You could use exec (nodejs docs) to run a mongo script inside of meteor inside of a Meteor.startup
on the server.
示例:
Meteor.startup(function () {
var exec = Npm.require('child_process').exec;
exec('mongo localhost:27017/meteor path-to/my-insert-script.js', function ( ) {
// done
});
});
不是最佳选择,但我认为这是您目前最好的选择.您还可以在exec中对Mongo使用命令行选项--eval
,并将insert语句作为字符串传递给exec.可能看起来像这样:
Not optimum, but I think it's your best bet for now. You can also use the command line option --eval
against Mongo in exec and pass the insert statement as a string to exec. That might look like this:
Meteor.startup(function () {
var exec = Npm.require('child_process').exec;
exec('mongo localhost:27017/meteor --eval \'db.Orders.insert(' + JSON.stringify(arrOfOrders) + ')\'', function ( ) {
// done
});
});
这篇关于在流星或节点中批量插入mongodb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!