Does inserting multiple documents in a Meteor Collection work the same as pure mongodb?

前端 未结 4 942
终归单人心
终归单人心 2021-02-13 04:52

It seems I can\'t do a multiple insert in Meteor the same way it is described here in the Mongodb documentation...

In my js console :

> Test.insert([{na

4条回答
  •  太阳男子
    2021-02-13 05:08

    You should always use bulk insert for these things. Meteor doesn't support this out of the box. You could use the batch insert plugin or access the node Mongodb driver to do it natively:

    var items = [{name:'hello'},{name:'hello again'}],
    
        testCollection = new Mongo.Collection("Test"),
        bulk = testCollection.rawCollection().initializeUnorderedBulkOp();
    
    for (var i = 0, len = items.length; i < len; i++) {
        bulk.insert(  items[i] );
    }
    
    bulk.execute();
    

    Note that this only works on mongoDB 2.6+

提交回复
热议问题