Bulk mongodb insert in Meteor or Node

后端 未结 3 1774
情话喂你
情话喂你 2020-12-20 03:20

MongoDB supports bulk insert http://docs.mongodb.org/manual/core/bulk-inserts/

I have tried it in Meteor collection:

Orders.insert([
    { \"cust_id\         


        
3条回答
  •  误落风尘
    2020-12-20 04:04

    You could use exec (nodejs docs) to run a mongo script inside of meteor inside of a Meteor.startup on the server.

    Example:

    Meteor.startup(function () {
        var exec = Npm.require('child_process').exec;
        exec('mongo localhost:27017/meteor path-to/my-insert-script.js', function ( ) {
           // done
        });        
    });
    

    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
        });        
    });
    

提交回复
热议问题