Sunday, April 10, 2016

Mongo DB Query

Ø  Find all collection names:  To find all collection names in mongo database use the below query.

db.getCollectionNames()

Return Type:  Array

Ø  Find all documents from collection name: To find document from collection name use the below query.

db.<<collectionName>>.find()

Return Type:  Cursor (i.e. collection of objects)

Ø  Find document(s) based on filter criteria: To find document based on filter criteria use the below query.

db.<<collectionName>>.find({ <field1>: <value1>, <field2>: <value2>, ... })

For the nested field use the dot(.) operator with value to find the document.

Return Type:  Cursor (i.e. collection of objects)

Ø  Find document(s)  using operators: To create condition with operator use the below syntax:

db.<<collectionName>>.find({  <field1>: { <operator1>: <value1> }  })

Operators are: $lt, $gt,

For AND Operator:  Use comma (,) 
And
For OR Operator:  Use $OR

 db.<<collectionName>>.find( {  $or:  [  { field1: value1 }, { field2: value2 }  ]  } )

Return Type:  Cursor (i.e. collection of objects)

Ø  Sort document in collection: To sort document in collection use the below query.

db.<<collectionName>>.find().sort({ “field1” : 1, “field2”: -1})
1 is used for Ascending and -1 is used for Descending.

Return Type:  Cursor (i.e. collection of objects)

Note: “Field Name” is case sensitive

Ø  Remove document from collection: To remove document from collection use the below query.

db.<<collectionName>>.remove({“fieldname”:”value”})



track by in angular js

What is the use of track by in angular js ?

Angular js uses key to associate the DOM element with the items in the model or collection that you are using in ng-repeat.

You cannot use duplicate key in angular js while using ng-repeat. To avoid this problem, you can use "track by" with ng-repeat.

In track by you have to use angular expression that will be used to uniquely identify the each items in collection or model. "track by" tells the angular js that how angular js will track the association between DOM and the model (i.e. collection). Internally angular js uses "track by $id(obj)" for this purpose. You can use track by $index if you do not have a unique identifier.

If you are using "track by" then it will avoid generating DOM elements every times for element that are already rendered. It improves the rendering performance.

Run the below example:  
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Track By</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
</head>
<body ng-app="">
<div ng-repeat="item in [12,15,15,11]">
{{item}}
</div>
<div ng-repeat="item in [12,15,15,11] track by $id(obj)">
{{item}}
</div>
<div ng-repeat="item in [12,15,15,11] track by $index">
{{item}}
</div>
</body>
</html>


Note:

1.    ng-repeat in First and second div will throw following error (you can check in your browser console)

"Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: item in [12,15,15,11] track by $id(obj), Duplicate key: undefined:undefined, Duplicate value: 15"

It is because of duplicate item in array [12,15,15,11].

2.    Angular Js internally use $id(obj) for track by.

3.    ng-repeat in third div will not throw error and work properly because we have used "track by $index". It will solve the problem of duplicate key.

Hope you have enjoyed. Thanks for reading.


Bootstrap Calender

This example shows how an user can use bootstrap calender.

You can use custom format also. For more reference visit to the moment js website http://momentjs.com/
https://know4grow.blogspot.com

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Bootstrap Calender</title>

    <link rel="stylesheet" type="text/css" media="screen" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" />

    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">

    <link href="http://cdn.rawgit.com/Eonasdan/bootstrap-datetimepicker/e8bddc60e73c1ec2475f827be36e1957af72e2ea/build/css/bootstrap-datetimepicker.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
    <![endif]-->

    <script type="text/javascript" src="http://code.jquery.com/jquery-2.1.1.min.js"></script>

    <script type="text/javascript" src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>

    <script src="http://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment-with-locales.js"></script>

    <script src="http://cdn.rawgit.com/Eonasdan/bootstrap-datetimepicker/e8bddc60e73c1ec2475f827be36e1957af72e2ea/src/js/bootstrap-datetimepicker.js"></script>

</head>
<body>
    <div class="container">
        <div class="row">
            <div class='col-sm-6'>
                <div class="form-group">
                    <div class='input-group date' id='myDatePicker'>
                        <input type='text' class="form-control" />
                        <span class="input-group-addon">
                            <span class="glyphicon glyphicon-calendar"></span>
                        </span>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- <script src="js/moment.js"></script> -->

    <script type="text/javascript">
        $(function () {
            $('#myDatePicker').datetimepicker({
                locale: 'en-GB',
                format: 'DD-MMM-YYYY h:mm:ss A' //here  you can change the date format as per your requirement. Please refer http://momentjs.com/
            });
        });
    </script>
</body>
</html>