These are chat archives for Automattic/mongoose

25th
Dec 2016
Yuval
@ValYouW
Dec 25 2016 21:15

Hi,
I am trying to find out what is the best option to do a bulk upsert, I have a "tags" collection and basically all I want to do is insert only new tags when I get bunch of those, what I did is:

var bulk = TagModel.collection.initializeOrderedBulkOp();
for (var i = 0; i < tags.length; i++) {
    bulk.find({name: tags[i]}).upsert().updateOne({
        $setOnInsert: { name: tags[i], name_lower: tags[i].toLowerCase() }
    });
}

bulk.execute(...)

Is this a valid approach? as basically I am not using mongoose here but the driver correctly (right?).
Thx