Category Archives: Q&A

How to transfer contexts among separate node.js environments? [closed]

Lets suppose I have my app running on machine A, at some point I need to shut down that machine, and transfer my app to machine B to continue its process. so I need to be able to save and restore context of my app.

How can implement that?

To be more specific, my question is: How can I save/restore application context in node.js?

What does "the same" refer to in this quote from the Reactive Manifesto? [closed]

This is the first few lines of the Reactive Manifesto:

Organisations working in disparate domains are independently discovering patterns for building software that look the same. These systems are more robust, more resilient, more flexible and better positioned to meet modern demands.

In the first sentence what does "look the same" refer to? "Patterns" or "software"?

What does "the same" refer to in this quote from the Reactive Manifesto?

This is the first few lines of the Reactive Manifesto:

Organisations working in disparate domains are independently discovering patterns for building software that look the same. These systems are more robust, more resilient, more flexible and better positioned to meet modern demands.

In the first sentence what does "look the same" refer to? "Patterns" or "software"?

Preserve `this` with recursive setImmediate()

Salam (means Hello:))

In my node.js app, I need to use setImmediate() to recessively call a function and keep its context intact for next execution.

Consider following example:

var i=3;

function myFunc(){
    console.log(i, this);
    --i && setImmediate(arguments.callee);
}

myFunc();

Output:

3 // a regular `this` object
2 { _idleNext: null, _idlePrev: null, _onImmediate: [Function: myFunc] }
1 { _idleNext: null, _idlePrev: null, _onImmediate: [Function: myFunc] }

As you can see, after first execution this is overwritten. How should I work around this?

Preserve `this` with recursive setImmediate()

In my node.js app, I need to use setImmediate() to recessively call a function and keep its context intact for next execution.

Consider following example:

var i=3;

function myFunc(){
    console.log(i, this);
    --i && setImmediate(arguments.callee);
}

myFunc();

Output:

3 // a regular `this` object
2 { _idleNext: null, _idlePrev: null, _onImmediate: [Function: myFunc] }
1 { _idleNext: null, _idlePrev: null, _onImmediate: [Function: myFunc] }

As you can see, after first execution this is overwritten. How should I work around this?

how to update fields of a nested object in mongodb?

Salam (means Hello) :)

I'm using node-mongodb-native driver and I need to update specific fields of objects stored in an array. this is a sample document in my mongodb collection:

{
    "fields" : [ 
        {
            "en" : "birthDate",
            "status" : "enable",
            "index" : 10
        },{
            "en" : "email",
            "status" : "enable",
            "index" : 4
        },{
            "en" : "inviterCode",
            "status" : "enable",
            "index" : 2
        }
    ]
}

and this is my new data:

var newData = [ 
    {
        "en" : "birthDate",
        "status" : "disable",
    },{
        "en" : "email",
        "status" : "disable",
    }
];

as you can see, if en field matches, status field should be updated. I know that following query works for a single update, but how can I achieve multiple updates with a single query?

collection.update(
    {'fields.en': 'birthDate'}, 
    {$set:{'fields.$.status': 'disable'}}, 
    {w:1, multi: true}, 
    function(error, count){

    }
);

how to update fields of a nested object in mongodb?

Salam (means Hello) :)

I'm using node-mongodb-native driver and I need to update specific fields of objects stored in an array. this is a sample document in my mongodb collection:

{
    "fields" : [ 
        {
            "en" : "birthDate",
            "status" : "enable",
            "index" : 10
        },{
            "en" : "email",
            "status" : "enable",
            "index" : 4
        },{
            "en" : "inviterCode",
            "status" : "enable",
            "index" : 2
        }
    ]
}

and this is my new data:

var newData = [ 
    {
        "en" : "birthDate",
        "status" : "disable",
    },{
        "en" : "email",
        "status" : "disable",
    }
];

as you can see, if en field matches, status field should be updated. I know that following query works for a single update, but how can I achieve multiple updates with a single query?

collection.update(
    {'fields.en': 'birthDate'}, 
    {$set:{'fields.$.status': 'disable'}}, 
    {w:1, multi: true}, 
    function(error, count){

    }
);

Why serving files with nodejs is faster than apache when I use a single mongodb connection? [closed]

Salam (means Hello) :)

I'm trying to improve speed of my nodejs website which depends on some mongodb operations and serving some static files. I'm using node-mongodb-native driver. as far as I know:

  1. Nodejs is slow on serving static files, nginx or apache are better options for this task.
  2. Caching a single db object and using it during application run time, is faster than calling MongoClient.connect() for every database operation.

nginx is not currently an option for me. So using a single db connection along side using apache for static files should be fastest combination, but tests on a semi-production environment don't verify this theory:

file server / db connection : speed  

nodejs / multiple : 24895ms
apache / multiple : 10770ms

nodejs / single   :  7068ms
apache / single   : 14995ms

So why when using multiple connections, apache makes my application load faster, and when using a single connection, apache makes my application load slower?