Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs mongoose bulk update

I have a collection of documents and I need to add a new field for ever document. If I run a query to get all documents and then update every single one node.js is stopped, may be for memory leak

This is my code

var express = require('express');

var geocoderProvider = 'google';
var httpAdapter = 'http';

var People = require("./models/people").collection.initializeOrderedBulkOp();

var app = express();

var geocoder = require('node-geocoder').getGeocoder(geocoderProvider, httpAdapter, {});

app.get('/', function (req, res) {
  People.find({}, function (err, docs) {
    if (err) {
      res.send(err);
    }else{
      docs.forEach( function (doc){
        geocoder.geocode({address: doc.address, country: 'Italy', zipcode: doc.cap}, function(error, value) {
          doc.loc.coordinates[0]=value[0].latitude;
          doc.loc.coordinates[1]=value[0].longitude;
          People.update({ _id: doc._id }, { $set: { loc: doc.loc }}, { multi: true }, function (error){
            if(error){
              console.error('ERROR!');
            }
          });
        });
      });
    }
  });
});

var server = app.listen(3000, function () {
  var host = server.address().address
  var port = server.address().port
  console.log('Example app listening at http://%s:%s', host, port)
});

There is any way to bulk update with mongoose? Thanks in advance

like image 439
Claudio Pomo Avatar asked Jan 29 '15 15:01

Claudio Pomo


3 Answers

More detailed info about the query and update query.

var bulk = People.collection.initializeOrderedBulkOp();
    bulk.find(query).update(update);
    bulk.execute(function (error) {
       callback();                   
    });

Query is searching with array.
Update needs a $set

var bulk = People.collection.initializeOrderedBulkOp();
    bulk.find({'_id': {$in: []}}).update({$set: {status: 'active'}});
    bulk.execute(function (error) {
         callback();                   
    });

Query is a searching the id

var bulk = People.collection.initializeOrderedBulkOp();
    bulk.find({'_id': id}).update({$set: {status: 'inactive'}});
    bulk.execute(function (error) {
         callback();                   
    });
like image 195
Mangesh Pimpalkar Avatar answered Oct 03 '22 22:10

Mangesh Pimpalkar


You can drop down to the collection level and do a bulk update. This action will not be atomic - some of the writes can fail and others might succeed - but it will allow you to make these writes in a single round trip to your database.

It looks like this:

var bulk = People.collection.initializeUnorderedBulkOp();
bulk.find({<query>}).update({<update>});
bulk.find({<query2>}).update({<update2>});
...
bulk.execute(function(err) {
    ...
});

Check out the docs here: http://docs.mongodb.org/manual/core/bulk-write-operations/

like image 24
jtmarmon Avatar answered Oct 02 '22 22:10

jtmarmon


This example should include all the cases that we can mix together using directly with Mongoose bulkWrite() function:

Character.bulkWrite([
  {
    insertOne: {
      document: {
        name: 'Eddard Stark',
        title: 'Warden of the North'
      }
    }
  },
  {
    updateOne: {
      filter: { name: 'Eddard Stark' },
      // If you were using the MongoDB driver directly, you'd need to do
      // `update: { $set: { title: ... } }` but mongoose adds $set for
      // you.
      update: { title: 'Hand of the King' }
    }
  },
  {
    deleteOne: {
      {
        filter: { name: 'Eddard Stark' }
      }
    }
  }
]).then(res => {
 // Prints "1 1 1"
 console.log(res.insertedCount, res.modifiedCount, res.deletedCount);
});

Official Documentation: https://mongoosejs.com/docs/api.html#model_Model.bulkWrite

like image 31
Hemã Vidal Avatar answered Oct 04 '22 22:10

Hemã Vidal