MongoDB Local Installation and Setup

  • Download MongoDB server from the https://www.mongodb.com/download-center  and install. ( i prefer community edition ).
  • For these instructions, let us assume that you have installed MongoDB to C:\ProgramFiles\MongoDB\Server\3.4\.
  • MongoDB requires a data directory to store all data, so lets create a folder and point the data path to that folder.
"C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe" --dbpath d:\test\mongodb\data
  • Now Start the MongoDB. Run the following code in command prompt.
"C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe"

This starts the main MongoDB database process. The waiting for connections message in the console output indicates that the mongod.exe process is running successfully.

  • To connect to MongoDB through the mongo.exe shell, open another Command Prompt.
"C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe"
  • In the Above mongo shell you can run some commands and play through it like ,
    • show dbs – Shows list of database
    • use dbName – Switch to that database
    • db.stats() – Gives the db status
    • show collections – Shows list of collections
    • db.collectionName.find() – List all the data in the collection

For More commands refer https://docs.mongodb.com/manual/mongo/

For Accessing this MongoDB with Local Node ( Refer Link )

Advertisements

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.