Category Archives: Stack Overflow

How to update multiple documents in mongodb native nodejs driver?

Salam (means hello) :)

I'm running mongodb 2.4.8 and Mongo DB Native NodeJS Driver. when I use the following function, only first document that matched query updates. how can I update all matching documents?

function update(coll, query, update, callback){
    var options = options || {};
    MongoClient.connect('mongodb://127.0.0.1:27017/dbName', function(error, db) {
        if(error){
            return console.dir(error);
        }
        db.collection(coll).update(query, update, {w:1}, function(error, result) {
            callback(error, result);
        });
    });
}

I installed my mongodb driver via npm install mongodb command, which installs version 1.3.23, does this driver version support multi update? if not, how can I install a newer version of driver supports multi update?

How to update multiple documents in mongodb native nodejs driver?

Salam (means hello) :)

I'm running mongodb 2.4.8 and Mongo DB Native NodeJS Driver. when I use the following function, only first document that matched query updates. how can I update all matching documents?

function update(coll, query, update, callback){
    var options = options || {};
    MongoClient.connect('mongodb://127.0.0.1:27017/dbName', function(error, db) {
        if(error){
            return console.dir(error);
        }
        db.collection(coll).update(query, update, {w:1}, function(error, result) {
            callback(error, result);
        });
    });
}

I installed my mongodb driver via npm install mongodb command, which installs version 1.3.23, does this driver version support multi update? if not, how can I install a newer version of driver supports multi update?

Serving zipped files in nodejs, how to cache instead of recompress?

Salam (means Hello) :)

I'm developing a static file server that must support data-encoding: gzip, the example in zlib documentation is working fine for me, but I need to save zipped files to disk, to avoid re-compressing for future requests. here is my code, which doesn't work:

http.createServer(function(request, response) {
    var url = my_normalizer(request.url);
    var acceptEncoding = request.headers['accept-encoding'] || '';

    if(acceptEncoding.match(/\bgzip\b/)){
        try{
            var zipped = fs.createReadStream(url +'.gz');
        }catch(e){
            var inp = fs.createReadStream(url);
            var out = fs.createWriteStream(url+'.gz');
            inp.pipe(zlib.createGzip()).pipe(out);
            var zipped = fs.createReadStream(url +'.gz');
        }
        response.writeHead(200, { 'content-encoding': 'gzip' });
        zipped.pipe(response);
    }
}).listen(80);

and this is the result in firefox:

Content Encoding Error

The page you are trying to view cannot be shown because it uses an invalid or unsupported form of compression.

for what it's worth, a .gz version of the requested file is correctly generated and saved to disk.