How to do bulk modifications in mongodb


#1

Hi all.

I create a collection with:

import { Mongo } from ‘meteor/mongo’
import SimpleSchema from ‘simpl-schema’

export const Tx = new Mongo.Collection(‘tx’);
Tx.schema = new SimpleSchema({
comment: {type: String},

I have tried to run bulk updates with:

let bulk = Tx.initializeOrderedBulkOp()
bulk.find( { field1: val1, field2: val2 } ).update( { $set: { field3: val3} } )

But I get error: “initializeOrderedBulkOp is not a function”.
I have tried diferent places to execute initializeOrderedBulkOp with no result.

How do I do bulk updates in meteor-vue?
Thanks.


#2

You can only access initializeOrderedBulkOp through the underlying NPM library. In Meteor, that’s done using the Collection.rawCollection() method:

const bulk = Tx.rawCollection().initializeOrderedBulkOp();

However, you should be aware that because these methods do not use Meteor’s MongoDB package, your simple-schema will not be applied. In addition, these methods return Promises, and so you will find it simplest to use async/await syntax, which plays nicely with Meteor’s server-side Fibers.


#3

Thanks.

I am still trying to understand the async/await thing and meteor’s inner working.
I am getting errors so temporarely I’ll do it one by one.


#4

You may find this helpful:

That article uses a MySQL database to illustrate the concepts, but the Promises returned by MongoDB can be handled in the same way.


#5

Thank you Rob.

Have a nice day.