Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't execute more than 1000 update operations in Bulk #337

Closed
ChuanyuWang opened this issue Sep 25, 2017 · 5 comments
Closed

Can't execute more than 1000 update operations in Bulk #337

ChuanyuWang opened this issue Sep 25, 2017 · 5 comments

Comments

@ChuanyuWang
Copy link
Contributor

ChuanyuWang commented Sep 25, 2017

Summary

Can't update more than 1000 times by mongojs.Bulk, it works with mongodb
According to this doc, Bulk support more than 1000 operations, which will be grouped into 1000 or less batch.

Environment

  • mongojs: 2.4.1
  • mongoDB server: 3.2.5
  • nodejs: 6.9.1
  • OS: Window 10 64bit

Code Snippet to reproduce

var MongoClient = require('mongodb').MongoClient;
var mongojs = require('mongojs');
var connectURL = 'mongodb://admin:pass@localhost:27017/bulk_test?authSource=admin';
// start testing
prepare1001Documents();
setTimeout(testNativeMongoDB, 1500); // wait 1.5 second for above testing completed
setTimeout(testMongoJS, 3000); // wait 1.5 second for above testing completed

function prepare1001Documents() {
    MongoClient.connect(connectURL, function (err, db) {
        if (err) return console.error(err);

        var col = db.collection('test_over_1000_write');
        col.count({a: {$gte: 0}}, function (err, count) {
            if (count > 1000) return db.close();

            // Initialize the unordered Batch
            var batch = col.initializeUnorderedBulkOp();
            for (var i = 0; i < 1001; i++) 
                batch.insert({a: i});

            // Execute the operations
            batch.execute(function (err, result) {
                if (err) return console.error(err);
                // Check state of result
                if (result.ok) console.info("insert 1001 documents successfully");
                db.close();
            });
        });
    });
}

function testNativeMongoDB() {
    MongoClient.connect(connectURL, function (err, db) {
        if (err) return console.error(err);
        // Get the collection
        var col = db.collection('test_over_1000_write');
        // Initialize the unordered Batch
        var batch = col.initializeUnorderedBulkOp();
        for (var i = 0; i < 1001; i++) 
            batch.find({a: i}).updateOne({$set: {b: "update by mongodb"}});

        // Execute the operations
        batch.execute(function (err, result) {
            if (err) return console.error(err);
            // Check state of result
            if (result.ok) console.info("update 1001 documents successfully by mongodb\n");
            db.close();
        });
    });
}

function testMongoJS() {
    var db = mongojs(connectURL);
    db.on('error', function (err) {
        console.error('connect database "%s" with error', db.toString(), err);
    })
    var col = db.collection('test_over_1000_write');
    // Initialize the unordered Batch
    var batch = col.initializeUnorderedBulkOp();
    for (var i = 0; i < 1001; i++)
        batch.find({a: i}).updateOne({$set: {b: "update by mongojs"}});

    // Execute the operations
    batch.execute(function (err, result) {
        if (err) return console.error(err);
        // Check state of result
        if (result.ok) console.info("update 1001 documents successfully by mongojs");
        db.close();
    });
}

Output

insert 1001 documents successfully
update 1001 documents successfully by mongodb

{ MongoError: exceeded maximum write batch size of 1000
    at Function.MongoError.create (C:\github\mongo-test\node_modules\mongodb-core\lib\error.js:31:11)
    at C:\github\mongo-test\node_modules\mongodb-core\lib\connection\pool.js:497:72
    at authenticateStragglers (C:\github\mongo-test\node_modules\mongodb-core\lib\connection\pool.js:443:16)
    at Connection.messageHandler (C:\github\mongo-test\node_modules\mongodb-core\lib\connection\pool.js:477:5)
    at Socket.<anonymous> (C:\github\mongo-test\node_modules\mongodb-core\lib\connection\connection.js:331:22)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at Socket.Readable.push (_stream_readable.js:134:10)
    at TCP.onread (net.js:548:20)
  name: 'MongoError',
  message: 'exceeded maximum write batch size of 1000',
  ok: 0,
  code: 16,
  errmsg: 'exceeded maximum write batch size of 1000' }
saintedlama added a commit that referenced this issue Sep 25, 2017
@saintedlama
Copy link
Collaborator

What version of mongojs are you using? I added a test test\test-excessive-bulk-update.js to verify the bug but the test passes.

@ChuanyuWang
Copy link
Contributor Author

both mongojs@2.4.1 and mongodb@2.2.31 version are update to date.
I don't know how you pass the test case, I discovery the same result on personal and company computers.
Or it's just me?!

@saintedlama
Copy link
Collaborator

You can run the tests if you clone the repo, npm install and npm test where you cloned the repo.

@lionvs
Copy link
Contributor

lionvs commented Jan 26, 2018

It didn't work for me on mongojs: 2.4.1 but worked on the current repository.
Latest version 2.4.1 doesn't include some latest commits.

I think we need to push a new version to npm in order to fix it.
@saintedlama @mafintosh

@ChuanyuWang
Copy link
Contributor Author

Add my confirmation. Double testing against 2.4.1 and 2.4.2. It turns out the issue is fixed in 2.4.2.
Thanks~
(But tag 2.4.2 is missing)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants