Tuesday, May 19, 2015

MVC in AngularJS

Angular JS supports MVC style of application design. MVC means Model-View-Controller. Lets understands each of this in Angular JS.

Controller

Controller responsibility is to wire up the $scope (which holds your models) to your view. Angular Model lives inside a controller, you use $scope inside a controller as a glue between controller and view.
Controller contains business logic behind the application to decorate the scope with functions and values.

Example : controller creation

var app = angular.module(‘firstApp’, []);
app.controller(‘MyController’, function($scope) {
});
view raw gistfile1.js hosted with ❤ by GitHub

Model

A model in angular can be a primitive type such as string, number, boolean or a complex type such as object. In short model is just a plain javascript object.
Model lives inside a controller.
Scope is an object and models are the properties of a scope

Example : model creation

var app = angular.module(‘firstApp’, []);
app.controller(‘MyController’, function($scope) {
// model called ‘name’ which is a string
$scope.name = “Deepak Sisodiya”;
});
view raw gistfile1.js hosted with ❤ by GitHub
Here name is model of type string and name is the property of $scope object.

View

View is what the users see, in angular view is HTML

Example : view creation

<!DOCTYPE html>
<html ng-app="firstApp">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
</head>
<body ng-controller="MyController">
<p>Hello {{ name }}</p>
</body>
</html>
view raw gistfile1.html hosted with ❤ by GitHub

Lets combine all components that is Model-View-Controller

<!DOCTYPE html>
<html ng-app="firstApp">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
</head>
<body ng-controller="MyController">
<p>Hello {{ name }}</p>
<script>
var app = angular.module('firstApp', []);
app.controller('MyController', function($scope) {
$scope.name = "Deepak Sisodiya";
});
</script>
</body>
</html>
view raw gistfile1.html hosted with ❤ by GitHub

model contains your data and with the help of $scope in controller, your model data will be available to your view

Link of Model-View-Controller example

Resource
http://mrbool.com/mvc-in-angularjs/28962

Saturday, May 9, 2015

Convert the local time to another time zone in JavaScript

In JavaScript, Date() object get time from your local system. And what, when you deploy your website to server and your server is in the Germany then it get time according to the Germany time zone And you want Indian time whatever your server location.

I recently face this situation. I am working on a website which runs only 8PM to 4AM according to the Indian time zone. So I found a solution for this on the internet which i am writing here.

before that you have to understand the offset, In JavaScript we can get offset by getTimezoneOffset() method of Date object and it gives the difference between UTC time and your local time.

For Example
Your server has Singapore time zone and you want indian time.

var d = new Date();
var time = d.getTime();    // time in milliseconds

var offset = d. getTimezoneOffset();      // gives -480

This is the offset of Singapore and tells that Singapore is ahead of UTC time by 480 minutes
-(minus) specify ahead of UTC time and +(plus) specify behind of UTC time.
Like, if UTC time is 1 PM then Singapore time is 9 PM

var offset = offset * 60 * 1000;

So for getting UTC time

var UTC = time + offset;
This is the actual UTC time which is located in London at Greenwich from where all other country time is calculated.

Now we have UTC time, for getting Indian time we have to know the offset of the India. and Indian offset is +5.5 hours, it means india is 5.5 hours ahaed of UTC time, thus by adding this to UTC time we can get the Indian time

var indianOffset = 5.5 * 60 * 60 * 1000;    // in milliseconds

var IndianTime = UTC + indianOffset;

var nd = new Date(IndianTime);

// this nd is indian time.

Resource
http://www.techrepublic.com/article/convert-the-local-time-to-another-time-zone-with-this-javascript/

Saturday, May 2, 2015

Introduction to mongoDB database

In MongoDB

Collection    :   Table
Document    :   Row
Field            :   Column

1. Find all documents of items collection
1. db.items.find();
view raw gistfile1.js hosted with ❤ by GitHub

2. Find documents of items collection whose price is 10
db.items.find({ price : 10 });
view raw gistfile1.js hosted with ❤ by GitHub

3. Find documents of items collection whose price is greater than 20
db.items.find( { price : { $gt : 20 } });
view raw gistfile1.js hosted with ❤ by GitHub

4. Find documents of items collection whose price is greater than 20 and less than 100
db.items.find({ price : { $gt : 20, $lt : 100 } });
view raw gistfile1.js hosted with ❤ by GitHub

5. Loop through all documents of items collection and print JSON
var myCursor = db.items.find();
myCursor.forEach(printjson);
view raw gistfile1.js hosted with ❤ by GitHub

6. Find first two document of items collection
db.items.find().limit(2);
view raw gistfile1.js hosted with ❤ by GitHub

7. Skip first two document of items collection and display others
db.items.find().skip(2);
view raw gistfile1.js hosted with ❤ by GitHub

8. Insert a document in items collection
db.items.insert({
name : 'daal makhni',
itemCode : 'a1',
price : "120",
sellingPrice : "140",
restaurant : "5540ca56d1f925f156a8c33d"
});
view raw gistfile1.js hosted with ❤ by GitHub

9. Find first document of items collection
db.items.findOne(); is same as db.items.find().limit(1);
db.items.findOne();
view raw gistfile1.js hosted with ❤ by GitHub

10. Find all document of items collection where name is ‘daal’ or ‘daal makhni’
db.items.find({ name : { $in : ['daal', 'daal makhni'] } });
view raw gistfile1.js hosted with ❤ by GitHub

11. And condition
db.items.find({ name : 'daal', price : 10});
db.items.find({ $and : [{ name : 'daal', price : 10}] });
db.items.find({ $and : [ {name : 'daal'}, {price : 10} ] });
view raw gistfile1.js hosted with ❤ by GitHub

12. Or condition
db.items.find({ $or : [{name : 'daal'}, {price : 20}] });
view raw gistfile1.js hosted with ❤ by GitHub

13. $and and $or both condition
db.items.find({ type : 'veg', $or : [ { name : 'daal'}, {name : 'rice'} ] });
view raw gistfile1.js hosted with ❤ by GitHub

14. Start mongoDB shell
      This will connect you to running mongod instance
mongo (Enter this in terminal)
view raw gistfile1.js hosted with ❤ by GitHub

15. Show the list of databases
show dbs
view raw gistfile1.js hosted with ❤ by GitHub

16. Show current db
db
view raw gistfile1.js hosted with ❤ by GitHub

17. If database nite-foodie-internal is exist then this command switch to nite-foodie-internal database. If database nite-foodie-internal is not exist then this command create database named nite-foodie-internal
use nite-foodie-internal
view raw gistfile1.js hosted with ❤ by GitHub

18. Deleted all collection of current database
db.dropDatabase();
view raw gistfile1.js hosted with ❤ by GitHub

19. Show all collections of current database
show collections
view raw gistfile1.js hosted with ❤ by GitHub

20. Create collection items in current database
db.createCollection(‘items’);
view raw gistfile1.js hosted with ❤ by GitHub

21. Delete collection items of current database
db.items.drop();
view raw gistfile1.js hosted with ❤ by GitHub

22. Update query
      Update the title
db.COLLECTION_NAME.update(SELECTIOIN_CRITERIA, UPDATED_DATA);
db.items.update({ title : 'first title'}, { $set : { 'title' : 'new title' } });
view raw gistfile1.js hosted with ❤ by GitHub

23. Delete documents where title equal to ‘new title’ of items collection
db.items.remove({title : 'new title'});
view raw gistfile1.js hosted with ❤ by GitHub

24. Delete all documents of items collection
db.items.remove({});
view raw gistfile1.js hosted with ❤ by GitHub

25. Find all documents of items collection and display only name price and type of each document.
In MongoDB when you execute find() method, then it displays all fields of a document. To limit this you need to set list of fields with value 1 or 0. 1 is used to show the field while 0 is used to hide the field.
db.items.find({}, {name:1, price:1,type:1});
view raw gistfile1.js hosted with ❤ by GitHub

26. Sort is used to sort data
      1 is used for ascending order while -1 is used for descending order
db.items.find({}).sort({ 'name' : 1});
db.items.find({}).sort({ 'name' : -1});
view raw gistfile1.js hosted with ❤ by GitHub

27. This query will loop through all documents of collection items and sum all price of same type
{ "_id" : "egg", "total_price" : 30 }
{ "_id" : "non-veg", "total_price" : 20 }
{ "_id" : "veg", "total_price" : 161 }
db.items.aggregate([ { $group : { _id : "$type", total_price : { $sum :"$price" } } } ]);
view raw gistfile1.js hosted with ❤ by GitHub

28. Create backup
mongodump --collection COLLECTION_NAME --db DB_NAME
mongodump --db nite-foodie-internal
// create backup of nite-foodie-internal database
mongodump --collection items --db nite-foodie-internal
// create backup of items collection of nite-foodie-internal database
view raw gistfile1.js hosted with ❤ by GitHub

29. Restore database nite-foodie-internal to mongoDB
mongorestore dump/nite-foodie-internal
view raw gistfile1.js hosted with ❤ by GitHub

30. Returns the count of documents that would match a find() query
db.items.find({}).count();
view raw gistfile1.js hosted with ❤ by GitHub

31. Apply regex in mongoDB query
db.items.find({ cuisine : new RegExp('^Indian$', 'i') });
view raw gistfile1.js hosted with ❤ by GitHub

32. How to export MongoDB database in CSV format
mongoexport --host localhost --db databaseName --collection collectionName --csv --out fileName.csv --fields commaSeperatedFieldsName
mongoexport --host localhost --db nite-foodie-pro --collection customers --csv --out text.csv --fields number
view raw MongoDB.sh hosted with ❤ by GitHub