Simple App Using Bootstrap, AngularJS, NodeJS and MongoDB.

A simple try using Node and Mongo to develop a Finance APP. Posted in Git hub link HERE.

  • Frontend – HTML, Bootstrap.
  • Framework – AngularJS.
  • Backend – NodeJS.
  • Database – MongoDB.

MongoDB CRUD Using Mongoose

Initialize Mongoose and Schema

var mongoose = require('mongoose');
mongoose.connect('mongodb://YOURVALUES.mongolab.com:11111/NAME');
var schema = new mongoose.Schema({ name: 'string', account: 'string', date: 'string' });
var accountz = mongoose.model('accountz', schema);

Create

   var small = new accountz({
        name: "SAMZ",
        account: "Monthly",
        date: "29/12/2015"
    });
    small.save(function (err) {
        if (err){
            console.log("Error in Save");
        }else{
            console.log("Save Sucessfully");
        }
    });

Read

   accountz.find().exec(function(err, data){
        if (err){
            console.log("Error in Reading");
        }else{
            console.log("The value = " + data);
        }
    });

Update

   accountz.findOne({ "_id": "0023"}, function (err, doc){
        doc.name = editObj.name;
        doc.account = editObj.account;
        doc.date = editObj.date;
        doc.save(function (err) {
            if (err){
                console.log("Error in Updating");
            }else{
                console.log("Updated Sucessfully");
            }
        });
    });

Delete

    accountz.remove({ "_id": "0023"}).exec(function(err, data){
        if (err){
            console.log("Error in Deleting");
        }else{
            console.log("Deleting Sucessfully");
        }
    });

You can refer to my github project Explore Here .

Get Users of a Room in NodeJs-SocketIO

In a chat using NodeJs / SocketIO, to get the all user using a room can be found by following code,

function get_users_by_room(nsp, room) {
  var users = []
  for (var id in io.of(nsp).adapter.rooms[room]) {
    users.push(io.of(nsp).adapter.nsp.connected[id]);
  };
  var roomUsers = [];
  users.forEach(function(elem){
      roomUsers.push({
          id : elem.id,
          name : elem.name,
          roomName : elem.rName
      });
  });
  return roomUsers;
};

This can be used to find the online users.