Mirek Pe
Mirek Pe

Reputation: 13

mongoose multi update in one query

is there please any way to perform this query in mongoose?

this multi update is possible from mongodb v2.6

{
update: <collection>,
updates:
[
    { q: <query>, u: <update>, upsert: <boolean>, multi: <boolean> },
    { q: <query>, u: <update>, upsert: <boolean>, multi: <boolean> },
    { q: <query>, u: <update>, upsert: <boolean>, multi: <boolean> },
    ...
],
ordered: <boolean>,
writeConcern: { <write concern> }
}

i found this topic, but its pretty old: Mongodb multi update with different value

thx everyone for suggetions

Upvotes: 1

Views: 1371

Answers (2)

Mirek Pe
Mirek Pe

Reputation: 13

ok these are the results

var p_text_data = require("../public/import/p_100_k_text_cz.json"); //data with new vals
_und.map(p_text_data,function(vals,index) {

        var new_name = vals.name + something;

        e_textModel.collection.update({ 'id': vals.id }, { $set: { 'name': new_name } }, {upsert: false, multi: true}); 

});

using sinle update every iteration, updating 100k documents take about 7+ seconds

and next code, using bulk, take less than 3 seconds

var bulk = e_textModel.collection.initializeUnorderedBulkOp();
_und.map(p_text_data,function(vals,index) {
    ite++;

    var new_name = vals.name + something;

    bulk.find( { 'id': vals.id } ).update( { $set: { 'name': new_name } } );

});
bulk.execute();

EDIT:

$var bulk = e_textModel.collection.initializeOrderedBulkOp();

is even couple times faster then UnorderedBulkOp in this case

Upvotes: 0

vladzam
vladzam

Reputation: 5908

From the details that you provided, I assume that you would like to issue a series of update queries based on several different criteria and specific update values for each particular query.

Nevertheless, I will address both possible scenarios when it comes to updating multiple documents in MongoDB.

As I previously mentioned, if you would like to update multiple documents, there are two possible scenarios:

  • Update multiple documents that match one set of specific criteria, case in which you can use db.collection.update(), by specifying the multi parameter when you fire your operation Official MongoDB Docs
  • Use bulk.find.update() to chain several multi update operations and execute them in bulk Official MongoDB Docs

Upvotes: 1

Related Questions