cereal
July 29, 2019, 11:04am
1
I have the following code that isn’t performing very well, and I’d like to know if there’s a way in Mongo to do it without iteration the array of records.
games.forEach(g => {
const { appid, ...game } = g;
Game.upsert({ appid }, { $set: game });
});
appid
in this case is an index.
I want to know if I can run some query do accomplish this in one go, without the loop.
1 Like
cereal
July 29, 2019, 11:32am
2
I came up with a solution for this, using the MongoDB driver directly.
const bulk = Games.rawCollection().initializeUnorderedBulkOp()
games.forEach(g => {
const { appid, ...game } = g;
bulk.find({ appid }).upsert().updateOne({ $set: game });
});
Meteor.wrapAsync(bulk.execute, bulk)();
3 Likes
Yeah, that’s the right way to do bulk ops!
cereal
July 29, 2019, 1:11pm
4
I do want to note that this will do an insertion with ObjectIDs, which isn’t meteor’s default.
You can change you collection’s to use ObjectIDs with the idGeneration
option on a collection
export const Games = new Mongo.Collection('games', { idGeneration: 'MONGO' });
The User collection will always use strings, though. If you’re using the accounts module.
1 Like
Good point! That’s a big gotcha. I find ObjectIDs a bit of a pain to work with