Install MongoDB on OpenSuse

after install run

sudo nano /etc/mongodb.conf
disable security
# Security settings.
#security:
#  authorization: enabled

the restart service and run with config

mongod --config /etc/mongodb.conf

other commands

db.createUser(
  {
    user: "admin",
    pwd: "admin",
    roles: [ { role: "root", db: "admin" } ]
  }
);
db.createUser(
    {
      user: "mahmood",
      pwd: "12345",
      roles: ["readWrite"]
    }
);

References
https://docs.mongodb.com/v3.0/reference/configuration-options/
http://stackoverflow.com/questions/23943651/mongodb-admin-user-not-authorized
http://stackoverflow.com/questions/35881662/mongodb-error-not-authorized-to-execute-command
https://docs.mongodb.com/manual/tutorial/enable-authentication/
http://stackoverflow.com/questions/23003391/how-do-i-add-an-admin-user-to-mongo-in-2-6

Sessions in Express.js

var session = require('express-session');
const MongoStore = require('connect-mongo')(session);
app.use(session({
    key: 'ERP.Session',
    secret: '310E56DD8E7C',
    resave:true,
    saveUninitialized:true,
    store: new MongoStore({
        url: 'mongodb://localhost/ERP'
    })
}));

Setting Session Variables

req.session.name = 'Napoleon';
req.session['primary skill'] = 'Dancing';

Reading Session Variables

var name = req.session.name;
var primary_skill = req.session['primary skill'];

Updating Session Variables

req.session.skills.push('Baking');
req.session.name = 'Pedro';

Deleting Session Variables

delete req.session.name
delete req.session['primary skill'];

Deleting a Session

req.session.destroy();
req.session.destroy(function() {
  res.send('Session deleted');
});

References
http://expressjs-book.com/index.html%3Fp=128.html
https://github.com/expressjs/session
https://github.com/jdesboeufs/connect-mongo

Remove values from an existing array in MongoDB

Before

{
   _id: 1,
   fruits: [ "apples", "pears", "oranges", "grapes", "bananas" ],
   vegetables: [ "carrots", "celery", "squash", "carrots" ]
}
{
   _id: 2,
   fruits: [ "plums", "kiwis", "oranges", "bananas", "apples" ],
   vegetables: [ "broccoli", "zucchini", "carrots", "onions" ]
}
db.stores.update(
    { },
    { $pull: { fruits: { $in: [ "apples", "oranges" ] }, vegetables: "carrots" } },
    { multi: true }
)

After

{
  "_id" : 1,
  "fruits" : [ "pears", "grapes", "bananas" ],
  "vegetables" : [ "celery", "squash" ]
}
{
  "_id" : 2,
  "fruits" : [ "plums", "kiwis", "bananas" ],
  "vegetables" : [ "broccoli", "zucchini", "onions" ]
}

References
https://docs.mongodb.com/manual/reference/operator/update/pull/#pull
http://stackoverflow.com/questions/16959099/how-to-remove-array-element-in-mongodb

Setting up a Node.js Cluster

 

var cluster = require('cluster');
var numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
  // Fork workers.
  for (var i = 0; i < numCPUs; i++) {
    cluster.fork();
  }

   Object.keys(cluster.workers).forEach(function(id) {
    console.log("I am running with ID : "+cluster.workers[id].process.pid);
  });

  cluster.on('exit', function(worker, code, signal) {
    console.log('worker ' + worker.process.pid + ' died');
  });
} else {

      //Do further processing.
}

Express :

app.js

var express=require("express");
var app=express();

app.get('/',function(req,res){

          res.end("Hello world !");

});

app.listen(3000,function(){

          console.log("Running at PORT 3000");

});

cluster.js

var cluster = require('cluster');
var numCPUs = require('os').cpus().length;

if (cluster.isMaster) {

  for (var i = 0; i < numCPUs; i++) {
    cluster.fork();
  }

  cluster.on('exit', function(worker, code, signal) {
    console.log('worker ' + worker.process.pid + ' died');
  });
} else {

    //change this line to Your Node.js app entry point.
    require("./app.js");
}

References
https://codequs.com/p/BkCZ1VMY/setting-up-a-node-js-cluster/
https://www.sitepoint.com/how-to-create-a-node-js-cluster-for-speeding-up-your-apps/
https://nodejs.org/api/cluster.html