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

MongoDB Update Operators

Name Description
$inc Increments the value of the field by the specified amount.
$mul Multiplies the value of the field by the specified amount.
$rename Renames a field.
$setOnInsert Sets the value of a field if an update results in an insert of a document. Has no effect on update operations that modify existing documents.
$set Sets the value of a field in a document.
$unset Removes the specified field from a document.
$min Only updates the field if the specified value is less than the existing field value.
$max Only updates the field if the specified value is greater than the existing field value.
$currentDate Sets the value of a field to current date, either as a Date or a Timestamp.

References
https://docs.mongodb.com/manual/reference/operator/update/#update-operators

Install MongoDB as Windows Service

mkdir c:\data\db
mkdir c:\data\log

create a file at C:\mongodb\mongod.cfg

systemLog:
    destination: file
    path: c:\data\log\mongod.log
storage:
    dbPath: c:\data\db

Install the MongoDB service

"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install

Start the MongoDB service

net start MongoDB

Stop or remove the MongoDB service as needed

net stop MongoDB
"C:\mongodb\bin\mongod.exe" --remove

References :
https://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/