Mongo Bulk Operations: using collection.rawCollection() and initializeUnorderedBulkOp()

So for some heavy lifting server methods, which insert & update thousands of mongo items, I am using

const myQueue = myMeteorMongoCollection.rawCollection().initializeUnorderedBulkOp();

Then, I am using the bulk operations to “add” operations to my queue - and execute() it:

await myQueue.execute()

Which is very weird is, that all the helpers functions, like .tojson(), .toString() and (after executing the queue) .getOperations() … these all do not work!
As soon as I call e.g.

Meteor.setInterval(() => console.log('getOperations', myQueue.getOperations()), 500);

I end up with:

myQueue.getOperations is not a function

I don’t get it … what am I doing wrong?

2nd question:

I need to await my queue finishing, only afterwards I can call my seconds queue - how do I do that?
I tried a simple await, I also tried calling Meteor.wrapAsync(myQueue.execute, myQueue)(), but both do not really wait for the finish.

There isn’t a 1:1 correlation between the MongoDB shell commands and the MongoDB npm package. However, the bulk operations are not documented very well in there, either!

The following code snippet may help you get what you’re looking for:

import { Meteor } from 'meteor/meteor';
import { Stuff } from '/imports/both/Stuff';

Meteor.startup( async () => {
  const bulk = Stuff.rawCollection().initializeUnorderedBulkOp();

  for ( let i = 1; i <= 1500; i++) {
    bulk.insert({i});
  }

  const bulkWriteResult = await bulk.execute();
  console.log(bulkWriteResult.getRawResponse());
});

The bulkWriteResult from the execute contains several methods, which you may find useful. One of which is getRawResponse(), which corresponds to the shell’s getOperations().

As long as you await the next execute within the same async method, it will follow on correctly.

1 Like
Exception: myQueue.getRawResponse is not a function

:disappointed_relieved:

Maybe it has something to do that I am also using Grapher and collection2?

That’s not what I used - check my code.

My bad sorry … has been a very long day. Thanks a bunch for your help, works like a charm! :slight_smile:

1 Like