Tag Archives: node.js

Node.js Production Practices – Developer Center – Joyent

As part of our stewardship of Node.js®, this resource is dedicated to sharing tools and techniques we use at Joyent to operate Node.js in production. From coding styles and design considerations through debugging large distributed systems, we intend to document our Node.js development and production practices.

10 Node.js best practices you should follow – Innofied

Node.js is a platform built on Chrome’s JavaScript engine (i.e. v8 JavaScript Engine); it helps to develop fast, scalable network application. It is basically used in server side coding, handling AJAX requests, maintaining routes for different APIs and manipulating database. Node.js uses an event-driven, non blocking I/O model that makes it lightweight and efficient. Now without defining v8 the blog will remain incomplete. v8 is Google’s open source JavaScript engine which is written in C++. Best feature of v8 is: it can run independently, or can be embedded into any C++ application. Let’s come to the main topic; here are the Node.js best practices:

Best Practices for Node.js Development | Heroku Dev Center

For most of the nearly twenty years since its inception, JavaScript lacked many of the niceties that made other programming languages like Python and Ruby so attractive: command-line interfaces, a REPL, a package manager, and an organized open-source community. Thanks in part to Node.js and npm, today’s JavaScript landscape is dramatically improved. Web developers wield powerful new tools, and are limited only by their imagination. What follows is a list of tips and techniques to keep you and your node apps happy.

mixu.net

I am currently working at Trifacta. I was previously a Node.js software engineer at Zendesk. I wrote their real-time notification infrastructure (Radar) and many other things. Before that I was a doctoral student (Information Systems Science) at Aalto University School of Economics. I have a Masters in Cognitive Science from Helsinki University, and am proud to be a United World College graduate.

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?

How to check if a collection exists in Mongodb native nodejs driver?

I need to check if a collection exists on a certain database and create it if it doesn't. I know that

db.createCollection(collName, {strict:true}, function(error, collection))

checks for existance of collection collName before creating it and sets error object. but I need an independent function to check that.

How to check if a collection exists in nodejs native driver?

Salam (means Hello) :)

I need to check if a collection exists on a certain database and create it if it doesn't. I know that

db.createCollection(collName, {strict:true}, function(error, collection))

checks for existance of collection collName before creating it and sets error object. but I need an independent function to check that.

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?