Sunday 22 September 2019

MongoDB Interview Question And Answers for 1 Year Experience

MongoDB Interview Question And Answers for 1 Year Experience

Question: How do I query mongodb with like ?
Following are few example of like as compare to SQL Query
/*select * from users where name like "%B%";*/
db.users.find({name: /B/});

/*select * from users where name like "B%"; */
db.users.find({name: /^B/});

/*select * from users where name like "%B"; */
db.users.find({name: /B$/});



Question: How to execute case in sensitive query?
/*select * from users where name like "%B%" OR like "%b%"; */
db.users.find({name: /b/i});



Question: How to list all collections in the mongoDB?
Following are three different methods.
db.getCollectionNames()

show collections

show tables




Question: Pretty print in MongoDB shell?
Normally we use query as below:
db.users.find()

For Pretty display, use like below:
db.users.find().pretty()




Question: How to get the last N records?
db.users.find().sort({_id:-1}).limit(20);
/* -1 means descending order */




Question: How to export collection into CSV?
mongoexport --host localhost --db bank --collection users --csv --out e:/dump/mycsv.csv --fields name,age,gender



Question: How to import into collection from CSV?
mongoimport -d bank -c users --type csv --file e:/dump/mycsv.csv --headerline



Question: How can I rename a field for all documents in MongoDB?
db.students.updateMany( {}, { $rename: { "name": "full_name" } } )



Question: How to store date/time in mongodb
db.users.insert({date: ISODate("2014-02-10T10:50:42.389Z")})



Thursday 19 September 2019

MongoDB Cursor with Examples

MongoDB Cursor with Examples



Question: What is cursor in MongoDB?
Cursor is a pointer to the result set of a query.
When you run a query for documents we retrieve a pointer (cursor) to the results instead of all result return.


Question: Give example of Cursor?
var myCursor=db.users.find({"age":30});
while (myCursor.hasNext()) {
   print(tojson(myCursor.next()));
}




Question: How to get the data type of variable?
var data=10;
typeof data;

var data='10';
typeof data;

Output
number
string



Question: What is use of forEach with Cursor?
Iterates the cursor to each document.
Example 1
db.users.find({"age":30}).forEach(printjson);

Example 2
db.users.find({"age":30}).forEach(function(data){
    print(data.name);
});
Output
name1
name2
name3



Question: How to use limit with Query?
 
db.users.find({"age":30}).limit(5).pretty();
Now, cursor will return maximum 5 records.


Question: How to get results in Array instead of JSON?
 
db.users.find({"age":30}).toArray()



Question: How to get number of results from Query?
 
db.users.find({"age":30}).size(); //2



Question: How to sort records by name in Ascending order?
 
 db.users.find({"age":30}).sort({"name":1}).forEach(function(data){print(data.name);});



Question: How to sort records by name in Descending order?
 
 db.users.find({"age":30}).sort({"name":-1}).forEach(function(data){print(data.name);});



Question: How to skip 4th record from query?
 
 db.users.find({"age":30}).skip(4).forEach(function(data){print(data.name);});



Question: How to search records with indexing?
 
 db.users.find({"age":30}).hint({"age":1}).pretty();  



Wednesday 18 September 2019

What is sharding in MongoDB?

What is sharding in MongoDB?

Question: What is sharding in MongoDB?
Sharding is a method for distributing data across multiple machines and its MongoDB's approach to meeting the demands of data growth. With increase of the size of the data, a single machine may not be sufficient to store the data OR read and write data. to fix this problem there are two different methods.
Vertical Scaling: Increase the capacity of single server and increase the power.
  • Powerful CPU
  • Increase RAM
  • Increase storage

Horizontal Scaling: dividing the system dataset and load over multiple servers.
We can use multiple server for storing and read/data data.
Using multiple server is lower cost than single machine with high-end hardware but increase the complexity in infrastructure and maintenance for the deployment.


Question: What are MongoDB Query Capabilities?
  • Sorting on one/multiple fields with ascending/descending order.
  • Projection (Retrieving only specified fields from each document in the cursor, not the entire document).
  • Cursor skip() and limit() to easily implement pagination.
  • explain() will return the detail on query will help to optimize the query.
  • aggregate() function used for grouping the records. db.users.aggregate([{$match:{gender:"male"}}, { $group: { _id: null, count: { $sum: 1 } } }]);
  • $match and $unbind function.



Question: How to database details?
db.stats();

Output
{
  "db": "bank",
  "collections": 4,
  "objects": 12,
  "avgObjSize": 245.33333333333334,
  "dataSize": 2944,
  "storageSize": 28672,
  "numExtents": 4,
  "indexes": 2,
  "indexSize": 16352,
  "fileSize": 67108864,
  "nsSizeMB": 16,
  "extentFreeList": {
    "num": 0,
    "totalSize": 0
  },
  "dataFileVersion": {
    "major": 4,
    "minor": 22
  },
  "ok": 1
}



Question: How to collection details?
db.users.stats();
Output
{
  "ns": "bank.users",
  "count": 5,
  "size": 2480,
  "avgObjSize": 496,
  "numExtents": 1,
  "storageSize": 8192,
  "lastExtentSize": 8192,
  "paddingFactor": 1,
  "paddingFactorNote": "paddingFactor is unused and unmaintained in 3.0. It remains hard coded to 1.0 for compatibility only.",
  "userFlags": 1,
  "capped": false,
  "nindexes": 1,
  "totalIndexSize": 8176,
  "indexSizes": {
    "_id_": 8176
  },
  "ok": 1
}



Monday 16 September 2019

Angularjs listing example with JSON

Angularjs listing example with JSON



[script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"][/script]
[script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-animate.js"][/script]
[style]
    .secdsubmenu{position:absolute; right:22px; top:2px; border:1px solid #eee; background:#fff; z-index:99}
    .secdsubmenu ul {padding:10px; margin:0}
    .secdsubmenu ul li{list-style:none; font-size:13px; color:#333; padding:0; margin:0; white-space:nowrap; border-bottom:1px solid #eee; height:30px; line-height:30px; }
    .secdsubmenu ul li a{color:#333; text-decoration:none}
[/style]
<div ng-app="myAppName" ng-controller="myCtrl">
<div class="inputfield">
<input ng-model="$ctrl.query" placeholder="Search" type="text" />
    </div>
<table border="1" cellpadding="2" cellspacing="0" style="width: 500pxpx;">
        <tbody>
<tr>
            <th>Title <button ng-click="sortBy('Title')">&gt;</button></th>
            <th>Date &amp; Time</th>
            <th>Status</th>
            <th>Action</th>
        </tr>
<tr ng-repeat="x in names| filter:$ctrl.query |  orderBy:Title:reverse">
            <td>{{x.Title}}</td>
            <td>{{x.DateTime}}</td>
            <td>{{x.Status}}</td>

            <td><a href="https://www.blogger.com/blogger.g?blogID=5911253879674558037#" ng-click="mySubmenu = !mySubmenu">...</a>
                <br />
<div class="secdsubmenu" ng-show="mySubmenu">
<ul>
<li><a href="https://www.blogger.com/blogger.g?blogID=5911253879674558037#">Edit</a></li>
<li><a href="https://www.blogger.com/blogger.g?blogID=5911253879674558037#">Delete</a></li>
<li><a href="https://www.blogger.com/blogger.g?blogID=5911253879674558037#">View URL</a></li>
<li><a href="https://www.blogger.com/blogger.g?blogID=5911253879674558037#">Embeded Code</a></li>
</ul>
</div>
</td>
        </tr>
</tbody></table>
</div>
[script]
    var myApp = angular.module('myAppName', ['ngAnimate']);
    myApp.controller('myCtrl', function($scope, $http) {
        $http.get("load_data.php")
                .then(function(response) {
                    $scope.names = response.data.records;
                });
        $scope.mySubmenu = false;

        $scope.Title = 'title';
        $scope.reverse = true;

        $scope.sortBy = function(title) {
            $scope.reverse = ($scope.title === title) ? !$scope.reverse : false;
            $scope.title = title;
        };
    });
[/script]
JSON Data load_data.php
    
{ "records":[ 
{"Title":"Title 1","DateTime":"Jul 29 2016, 01:00 AM","Status":"Completed"}, 
{"Title":"Title 2","DateTime":"Jul 28 2016, 02:00 PM","Status":"Completed"}, 
{"Title":"Title 3","DateTime":"Jul 27 2016, 03:00 AM","Status":"Completed"}, 
{"Title":"Title 4","DateTime":"Jul 26 2016, 04:00 AM","Status":"Completed"}, 
{"Title":"Title 5","DateTime":"Jul 25 2016, 05:00 AM","Status":"Completed"}, 
{"Title":"Title 6","DateTime":"Jul 24 2016, 06:00 AM","Status":"Completed"}, 
{"Title":"Title 7","DateTime":"Jul 23 2016, 09:00 AM","Status":"Completed"},
]}

Thursday 12 September 2019

New PHP 7 Feature List

New PHP 7 Feature List

Question: Why we should use PHP 7 as compare to PHP5?
  1. Improved performance. Its twice as fast as compare to PHP 5.
  2. Lower Memory Consumption.
  3. Scalar type declarations.
  4. Return and Scalar Type Declarations.
  5. Consistent 64-bit support
  6. Many fatal errors converted to Exceptions
  7. Secure random number generator.
  8. Various old and unsupported SAPIs and extensions are removed from the latest version
  9. NULL coalescing operator removed
  10. Anonymous Classes Added.
  11. Zero cost asserts
  12. Use of  new Zend Engine 3.0



Question: What is PHPNG?
PHPNG branch has been merged into master and has been used as the base for PHP 7.0.
PHPNG target is improve the performance and memory usage efficiency of PHP.


Question: What is a PHP parser?
PHP parser is a software which understand your coding and convert to computer language So that it can be executed & gives a output in HTML.


Question: What is a Scalar type declarations?
Allowing/prohibited datatype declaration in functions know for Scalar type declarations.
There are two options in Scalar type declarations
1) Coercive Mode (default)
2) Strict Mode
function sum(int ...$ints) {
      return array_sum($ints);
   }
   print(sum(2, '20', 4.2)); //26.2



Question: How to use return type declarations?
 function returnIntValue(int $value): int {
      return $value;
   }
   print(returnIntValue(5));



Question: What is spaceship operator?
It is Operator which is used to compare two expressions.
It return -1, 0 or 1.
print( 1 <=> 1); //0
print( 1 <=> 2); //-1
print( 2 <=> 1); //1




Question: Can define accept an array?
Yes, It can. For Example:
define('CLIENTS', [
    'client 1',
    'client 2',
    'client 3'
]);



Question: What is use of Closure::call() in PHP7?
It is used to temporarily bind an object scope to a closure and invoke it.
For Example:
 class A {
      private $x = 1;
   }
   $value = function() {
      return $this->x;
   };
   print($value->call(new A));



Question: How to import import Classes using single use statement?
Before PHP 7
use com\mytestclass\ClassA;
use com\mytestclass\ClassB;
use com\mytestclass\ClassC as C;

In PHP 7
 use com\mytestclass\{ClassA, ClassB, ClassC as C};



Question: What are Deprecated features in PHP7?
Following are deprecated feature in PHP7.
  1. Old Style Constructure.
     class A {
          function A() {
             print('Deprecated');
          }
       }
    
  2. Static calls to non-static methods.
    class A {
          function b() {
             print('Deprecated');
          }
       }
       A::b();
    
    
  3. password_hash() salt



Question: What extension are removed ?
  1. ereg
  2. mssql
  3. mysql
  4. sybase_ct



Question: From where I can download PHP7?
http://php.net/downloads.php


Question: How to setup PHP7 with WampServer?
http://www.wampserver.com/en/


Question: What is Class Abstraction?
1. Classes defined as abstract may not be instantiated.
2. Class that contains at least one abstract method must also be abstract.
3. When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child.
4. if the abstract method is defined as protected, the function implementation must be defined as either protected or public, but not private.



Question: Tell me something about Interface?
We use "interface" keyword to create class.
All methods declared in an interface must be public.
To implement an interface, the implements operator is used.
We can include multiple interface while implementing



Question: What are Traits? Give example
Traits are a mechanism for code reuse in single inheritance languages such as PHP.
We can't create instance of a traits

We can create Traits as below:-
    trait Hello {
        public function sayHello() {
            echo 'Hello ';
        }
    }   


We can use multiple traits using comma like below
use Hello, World;
When two traits have same function, it would conflict but we can fix using insteadof like below
A::bigTalk insteadof B;
We can set the visibility of function using "as" like below
use HelloWorld { sayHello as protected; }

We can use one trait from another using "use"
we can also define abstract,static, data members, method




Question: What is Overloading?
Overloading in PHP provides means to dynamically create properties and methods.
These dynamic entities are processed via magic methods one can establish in a class for various action types.
__get(), 
__set(), 
__isset() 
__unset() 
__call()
__callStatic()


Wednesday 11 September 2019

What is difference between Mongoexport and Mongodump

What is difference between Mongoexport and Mongodump

Question: What is mongodump ?
mongodump is a utility for creating a binary export of the contents of a database. mongodump is used for full backup, partial backup, syncing from production to staging/development, and changing the storage engine.




Question: What are popular options available with mongodump?
--help for Help
--version Version of mongodb
--host Host of database
--version Version of mongodump
--port Specify the port
--username Specify the username
--password Specify the password
--db Specify the database name
--collection Specify the collection name
--query Specify the query
--queryFile Specify the queryFile name
--gzip Compress the output
--out Specify the output folder
--repair Repair the database name



Question: Give few example of mongodump
Run Below command from Administrator window
Backup of all database?
 mongodump 

Backup of Single database?
 mongodump --db bank

Backup of collection?
mongodump --db bank --collection users

How to repair the mongoDB?
mongod --dbpath  "c:/data/db" --repair



Question: What is mongoexport?
mongoexport is used to export the collections data to a JSON or CSV format.


Question: How to export collection using mongoexport?
mongoexport --db bank --collection users --out D:\mm\users.json



Question: How to export collection fields using mongoexport?
mongoexport --db bank --collection users --fields "age,gender,email" --out D:\mm\users_e.json



Question: How to export collection data on behalf of query?
mongoexport --db bank --collection users --query "{'age':'female'}" --out D:\mm\users_e.json



Tuesday 10 September 2019

MongoDB export and Import databases with Example

MongoDB export and Import databases

Following command will works from Administrator window.

Question: How to backup of all database?
 mongodump 



Question: How to Restore databases
mongorestore


Question: How to backup of Single database?
 mongodump --db bank



Question: How to Restore single database
mongorestore --db bank dump/bank



Question: How to backup of collection?
mongodump --db bank --collection users



Question: How to Restore single database
mongorestore --db testdb --collection users dump/bank/users.bson


Question: How to repair the mongoDB?
mongod --dbpath  "c:/data/db" --repair





Monday 9 September 2019

How Azure functions can convert mp4 to mp3 with node using ffmpeg?


Pre-requisite
  1. You have created a function with with name "convert-audio"
  2. You have downloaded and uploaded ffmpeg files (ffmpeg.exe) in "site -> wwwroot-> bin"
  3. You have created a folder with name wav_to_mp3 "site -> wwwroot-> wav_to_mp3"


Source Code
//Download the Required module
const http = require('https');
const fs = require('fs');
const childProcess = require('child_process');
//Define constants
const VIDEO_PATH = 'wav_to_mp3/';
const ffmpegLib='bin/ffmpeg';

module.exports =  async function (context, req) {
    ////////////// params declaration
    let bodyData=req.body;
    let audioFile=bodyData.audio_file;
    let videoFile=bodyData.video_file;
    let extension=bodyData.extension;
    
    if (audioFile && videoFile && extension) {
        if(fs.existsSync(VIDEO_PATH+videoFile)){
            await ffmpegConvertVideoToAudio(VIDEO_PATH+videoFile,VIDEO_PATH+audioFile,extension).then(data=>{
                messageData(200,{success:1,'msg':'',result:{audio_file:data.filename}});
           });            
        }else{
            messageData(200,{success:0,msg:"video_file does not exist",'result':{}});
        }

                   
    } else {
        messageData(200,{success:0,msg:"Parameter missing (audio_file OR video_file OR extension)",'result':{}});
    }
    
    //for print the message
    function messageData(statusCode,message){
        context.res = {
            status: statusCode,
            body: message,
            headers: {
                'Content-Type': 'application/json'
            }
        };   
         context.done();
    } 
    
};



///////////////////////////// Other Functions ////////////////////////////////////
function ffmpegConvertVideoToAudio(sourcefile, destinationFile, extension) {
    return new Promise(function(resolve, reject) {
        let result={success:0,'msg':'',filename:destinationFile};
        console.log('ffmpegConvertVideoToAudio called '+destinationFile);
          var args = [
              '-i', sourcefile,
              '-f', extension, destinationFile
          ];
          var proc = childProcess.spawnSync(ffmpegLib, args);   
            result.success=1
            resolve(result);   
    });

}    
///////////////////////////// Other Functions ////////////////////////////////////


URL: https://function-name.azurewebsites.net/api/convert-audio
Method: POST
Request: {"video_file":"0_20190821064650782.mp4","audio_file":"arun.mp3","extension":"mp3"} Response:
{
    "success": 1,
    "msg": "",
    "result": {
        "audio_file": "wav_to_mp3/arun.mp3"
    }
}



URL:
https://function-name.azurewebsites.net/api/convert-audio
Method: POST
Request: {"video_file":"0_20190821064650782.mp4","audio_file":"arun.flac","extension":"flac"} Response:
{
    "success": 1,
    "msg": "",
    "result": {
        "audio_file": "wav_to_mp3/arun.flac"
    }
}

MongoDB update and delete records with examples

MongoDB update and delete records with examples




Question: How to Update Single Record? (Even Multiple Found)
db.players.update({"age":29},{$set:{number:1000}})
It will delete single record.
Output
WriteResult({"nMatched":1,"nUpserted":0,"nModified":1})



Question: How to update Update multiple Record? - 1 way
db.players.update({"age":29},{$set:{number:100}},{multi:true})
It will delete multiple record.
Output
WriteResult({
  "acknowledged": true,
  "matchedCount": 3,
  "modifiedCount": 3
})



Question: How to Update multiple Record? - 2 Way
db.players.updateMany({"age":29},{$set:{number:135}})
Output
WriteResult({
  "acknowledged": true,
  "matchedCount": 3,
  "modifiedCount": 3
})



Question: How to remove a Record(s)?
db.players.remove({"_id" : ObjectId("57ce95da8a12a77c41313adc")});
Will Delete all the record with matched condition.
Output
WriteResult({"nRemoved":1})



Question: How to remove a Record(s) with limit?
db.players.remove({"age":30},2);
It will deleted only 2 records.
Output
WriteResult({"nRemoved":2})



Sunday 8 September 2019

Mongodb index commands with examples

Mongodb index commands with examples

Create OR Use existing Database
use bank;



Create a new collection.
db.createCollection("users");



Add Records in collection.
db.users.insert([{"isActive":false,"balance":"$3,960.64","age":30,"eyeColor":"blue","name":"Dawn Keith","gender":"female","company":"COSMOSIS","email":"dawnkeith@cosmosis.com","phone":"+1 (839) 437-3421","address":"392 Clifford Place, Fontanelle, Arizona, 2687"},{"isActive":false,"balance":"$1,280.14","age":31,"eyeColor":"green","name":"Bettie Eaton","gender":"female","company":"COMTREK","email":"bettieeaton@comtrek.com","phone":"+1 (861) 460-2317","address":"203 Allen Avenue, Elrama, North Carolina, 4453"},{"isActive":true,"balance":"$2,042.37","age":38,"eyeColor":"green","name":"Margie Ayala","gender":"female","company":"VOIPA","email":"margieayala@voipa.com","phone":"+1 (941) 569-2231","address":"111 Schroeders Avenue, Suitland, Louisiana, 7042"},{"isActive":false,"balance":"$3,170.35","age":37,"eyeColor":"blue","name":"Baker Townsend","gender":"male","company":"EVIDENDS","email":"bakertownsend@evidends.com","phone":"+1 (808) 500-2793","address":"190 Just Court, Canoochee, Alabama, 325"}]);



Get all records
db.users.find().pretty();




Analyse the Query
db.users.find().explain("executionStats");



Add Index on collection
db.users.ensureIndex({"age":1});



Get all indexes on collection.
db.users.getIndexes();
Output
[
  {
    "v": 1,
    "key": {
      "_id": 1
    },
    "name": "_id_",
    "ns": "bank.users"
  },
  {
    "v": 1,
    "key": {
      "age": 1
    },
    "name": "age_1",
    "ns": "bank.users"
  }
]



Delete Index on collection.
db.users.dropIndex({"age":1});
Output
{
  "nIndexesWas": 2,
  "ok": 1
}



Drop all indexes.
db.users.dropIndexes();
Output
{
  "nIndexesWas": 1,
  "msg": "non-_id indexes dropped for collection",
  "ok": 1
}



Check Indexes, after droping an index.
db.users.getIndexes();
Output
[
  {
    "v": 1,
    "key": {
      "_id": 1
    },
    "name": "_id_",
    "ns": "bank.users"
  }
]



ReIndex the collecton.
db.users.reIndex()
Output
{
  "nIndexesWas": 1,
  "nIndexes": 1,
  "indexes": [
    {
      "key": {
        "_id": 1
      },
      "name": "_id_",
      "ns": "bank.users"
    }
  ],
  "ok": 1
}



Simple Aggregate Example
db.users.aggregate({$group:{_id:"$age",total:{$sum:1}}});
Output
{
  "_id": 37,
  "total": 1
}{
  "_id": 38,
  "total": 1
}{
  "_id": 31,
  "total": 1
}{
  "_id": 30,
  "total": 2
}



Average Aggregate Example
db.users.aggregate({$group:{_id:"$gender",avgAge:{$avg:"$age"}}});
Output
{
  "_id": "female",
  "avgAge": 32.25
}{
  "_id": "male",
  "avgAge": 37
}



Max Value in Aggregate
db.users.aggregate({$group:{_id:"$gender",richest:{$max:"$balance"}}});
Output
{
  "_id": "female",
  "richest": "$3,960.64"
}{
  "_id": "male",
  "richest": "$3,170.35"
}



Friday 6 September 2019

Download the media files with node in Azure serverless

Download the media files with node in Azure serverless

Pre-requisite for Node in Azure Serverless 

  1. You have created a function with with name "download-files"
  2. You have downloaded ffmpeg files (ffmpeg.exe) in "site -> wwwroot-> bin"
  3. You have created a folder with name wav_to_mp3 "site -> wwwroot-> wav_to_mp3"


Source Code

//Download the Required module
const http = require('https');
const fs = require('fs');
//const childProcess = require('child_process');
const path = require('path');

//Define constants
const VIDEO_PATH = 'wav_to_mp3/';
const ffmpegLib='bin/ffmpeg';

module.exports =  function (context, req) {
    ////////////// params declaration
    let bodyData=req.body;
    let tourStreamName=bodyData.tour_stream_name;
    let videoUrls=bodyData.url;
    
    let filename='';
    let filenameSuccessLog={};
    let completedCounter=0;
    if (tourStreamName && videoUrls.length>0) {
        ////////////////////// ALL VIDEOS DOWNLOAD ////////////////////////////
        //const forLoop = async _ => {
         videoUrls.forEach(async (url) => {
              filename=path.basename(url); 
             // let videoDownload=VIDEO_PATH+filename;
                //full video url, filename, force download
                downloadFileAsync(url,filename,0).then(data=>{  
                   filenameSuccessLog[data.filename]=data.success;
                   completedCounter++;
                   if(videoUrls.length==completedCounter){
                       messageData(200,{success:1,'msg':'',result:filenameSuccessLog});
                   }                    
               }); 
          })
        //}    
         //forLoop();
        ////////////////////// ALL VIDEOS DOWNLOAD ////////////////////////////

    } else {
        messageData(200,{success:0,msg:"Parameter missing (tour_stream_name OR url)",'result':{}});
    }
    
    
    
    //for print the message
    function messageData(statusCode,message){
        context.res = {
            status: statusCode,
            body: message,
            headers: {
                'Content-Type': 'application/json'
            }
        };   
         context.done();
               
    } 
    
};



///////////////////////////// Other Functions ////////////////////////////////////
/**
 * @url: url of video
 * @destination: file with name download in wav_to_mp3
 * @forcedownload: 1 means download again even
 */
async function downloadFileAsync(url, destination,forcedownload) {
    return new Promise(function(resolve, reject) {
        let result={success:0,'msg':'',filename:destination};
        if (fs.existsSync(VIDEO_PATH+destination) && forcedownload==0) {
            result.success=1
            resolve(result);            
        }else{
            const file = fs.createWriteStream(VIDEO_PATH+destination);
            const request = http.get(url, (res) => {
              res.pipe(file);
            });
            request.once('error', (error) => 
              {
                  result.msg=error;
                  reject(result);
              }
            );
            file.once('finish', () => 
              {
                result.success=1
                resolve(result);
              }
            );             
        }
       
        
    })
}

///////////////////////////// Other Functions ////////////////////////////////////

URL: https://function-name.azurewebsites.net/api/download-files
Method: POST
Request:
{"tour_stream_name":"arun","url":["https://example.com/video/0_20190821064650782.mp4","https://example.com/video/tq_33038dev-GM-ma9kW-1106-26079.mp4"]}
Response:
{
    "success": 1,
    "msg": "",
    "result": {
        "tq_33038dev-GM-ma9kW-1106-26079.mp4": 1,
        "0_20190821064650782.mp4": 1
    }
}

Install FFMPEG in Azure Serverless Architecture

Install FFMPEG in Azure Serverless Architecture

Create "Function" in "Function App" in Azure (If you have not created)

https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-first-azure-function


Install FFMPEG in Azure Server

  1. Now Go to Function App, and Click on the your "Function App"
  2. Click on "Platform Features" then click on "Advance tools (Kudu)" like blow
  3. In Kudu, select CMD from the Debug console menu.
  4. Now, you can navigate to the folder by click on site -> wwwroot.

  5. Create a folder with bin,here we will put the ffmpeg library.
  6. a. Go to the https://ffmpeg.zeranoe.com/builds/ (for FFMPEG)
    b. Download the build as per Architecture.
    c. Unzip the downloaded content.
    d. Now copy the ffmpeg.exe, ffplay.exe and ffprobe.exe (from bin folder in downloaded).
    e. Copy these three files in site -> wwwroot-> bin folder.
  7. Create a folder with wav_to_mp3,here we will store the media like photo, videos etc.
  8. Here you can run the ffmpeg command such ffmpeg -version



Saturday 31 August 2019

MongoDB operators with examples

MongoDB operators with examples

Comparison Query Selectors

$eq  Equal to a specified value.
$gt  Greater than a specified value.
$gte  Greater than or equal to a specified value.
$lt  Less than a specified value.
$lte  Less than or equal to a specified value.
$ne  Not equal to a specified value.
$in  Matches any of the values specified in an array.
$nin  Matches none of the values specified in an array.

Example:
db.mycollectiontest.find('{"likes": {$gt:10}"likes"}'); 


Logical Query Selectors
$or  Joins query clauses with a logical OR Operator.
$and  Joins query clauses with a logical AND Operator.
$not  Inverts the effect of a query expression.
$nor  Joins query clauses with a logical NOR returns.

Example
db.mycollectiontest.find({"likes": {$gt:10}, $or: [{"by": "tutorials point"})


Element Query Selectors
$exists  Matches documents that have the specified field.
$type  Selects documents if a field is of the specified type.



Evalution Query Selector
$mod  Performs a modulo operation.
$regex  Specified regular expression.
$text  Performs text search.
$where  Matches documents that satisfy a JavaScript expression.



Array Query Selector
$all        Matches arrays that contain all elements specified in the query.
$elemMatch Selects documents if element in the array field matches all the specified $elemMatch conditions.
$size       Selects documents if the array field is a specified size.


Field Update Operators
$inc        Increments the value.
$mul        Multiplies the value.
$rename     Renames a field.
$setOnInsert Sets the value of a field if an update results in an insert of a document.
$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.





Monday 19 August 2019

MongoDb PHP functions Questions and Answers

MongoDb PHP functions Questions and Answers

Question: How to connect to MongoDb with PHP?
$mongoObj = new MongoClient();



Question: How to select database?
$mongoDbObj = $mongoObj->mydb;



Question: How to create collection?
$collection = $mongoDbObj->createCollection("mycollectiontest");



Question: How to select collection?
$collection= $mongoDbObj->mycollectiontest;



Question: How to insert document in collection?
/* 1 document */
$document = array( 
      "title" => "MongoDB", 
      "description" => "MongoDB database", 
      "likes" => 100,
      "unlikes", 50
   );
   $collection->insert($document);
/* 2 document */
$document = array( 
      "title" => "MongoDB2", 
      "description" => "MongoDB database2", 
      "likes" => 1002,
      "unlikes", 502
   );
   $collection->insert($document); 



Question: How to list all documents?
   $cursor = $collection->find();
   foreach ($cursor as $document) {
      print_r($document);
   }

Output
Array
(
    [_id] => MongoId Object
        (
            [$id] => 57c3e73d9ba8827012000029
        )

    [title] => MongoDB
    [description] => MongoDB database
    [likes] => 100
    [0] => unlikes
    [1] => 50
)
Array
(
    [_id] => MongoId Object
        (
            [$id] => 57c3e7e79ba8821815000029
        )

    [title] => MongoDB2
    [description] => MongoDB database2
    [likes] => 1002
    [0] => unlikes
    [1] => 502
)
Array
(
    [_id] => MongoId Object
        (
            [$id] => 57c3e8fb9ba882181500002a
        )

    [title] => MongoDB2
    [description] => MongoDB database2
    [likes] => 1002
    [0] => unlikes
    [1] => 502
)



Question: How to list all documents (2nd way)?
 $cursor = $mongoObj->mydb->{'mycollectiontest'}->find();
/*$mongoObj is clinet, mydb is database, mydb is collections*/
   foreach ($cursor as $document) {
      print_r($document);
   }



Question: How to list all documents (3rd way)?
$query = array('title' => 'MongoDB');
$cursor = $collection->find($query);
   foreach ($cursor as $document) {
      print_r($document);
   }



Question: How to delete a document?
 $collection->remove(array("title"=>"MongoDB2"));



Question: How to update a document?
  $collection->update(array("title"=>"MongoDB"), 
 array('$set'=>array("description"=>"This is updated description")));



Tuesday 13 August 2019

How do I add environment variables in Unix



Question: How to print the environment values?
printenv

OR
env



Question: How to SET the environment values For Korn shell (KSH)?
var=value
export varname



Question: How to SET the environment values For Bourne shell (sh and bash)?
export var=value



Question: How to SET the environment values For C shell (csh or tcsh)?
setenv var value



Question: What is .bashrc?
.bashrc is a shell script that Bash runs whenever it is started interactively.


Question: Can we set the environment value in .bashrc?
Yes, you can set the value in this also. like below
export PATH="$PATH:/some/addition"




Sunday 4 August 2019

How to post raw data using CURL in Zend Framework 2

How to post raw data using CURL in Zend Framework 2

Question: How to post raw data using Zend?
        $client = new Client('http://example.com/ajax/get-feedback-status', array(
            'maxredirects' => 0,
            'timeout' => 30
        ));

        // Performing a POST request
        $client->setMethod('POST');
        //Set data
        $client->setParameterGet(array(
            'first_name' => 'Web',
            'middle_name' => 'Technology',
            'last_name' => 'Experts',
            'category' => 'notes',
        ));
        $response = $client->send();
        if ($response->isSuccess()) {
            // the POST was successful
            echo $response->getBody();
        } else {
            echo 'Request is failed';
        }





Question: Can we post Multi dimensional array in Zend?
Yes, See example below.
        $client = new Client('http://example.com/ajax/get-feedback-status', array(
            'maxredirects' => 0,
            'timeout' => 30
        ));

        // Performing a POST request
        $client->setMethod('POST');
        //Set data
        $client->setParameterGet(array(
            'first_name' => 'Web',
            'middle_name' => 'Technology',
            'last_name' => 'Experts',            
            'post-ids' => array(10, 20, 30)
        ));



Question: How to add Cookie in httpClient Object?
        $client = new Client('http://example.com/ajax/get-feedback-status', array(
            'maxredirects' => 0,
            'timeout' => 30
        ));
        $cookieString = \Zend\Http\Header\SetCookie::fromString('Set-Cookie: flavor=chocolate%20chips');
        $client->addCookie($cookieString);



Question: How to change the HTTPS transport layer in httpClient Request?
        $config = array(
            'adapter' => 'Zend\Http\Client\Adapter\Socket',
            'ssltransport' => 'tls'
        );

        $client = new Client('http://example.com/ajax/get-feedback-status', $config);
        $cookieString = \Zend\Http\Header\SetCookie::fromString('Set-Cookie: flavor=chocolate%20chips');
        $client->addCookie($cookieString);



Question: How to call curl From proxy server?
        $config = array(
            'adapter'    => 'Zend\Http\Client\Adapter\Proxy',
            'proxy_host' => 'ADD HERE POST',
            'proxy_port' => 8000,
            'proxy_user' => 'USERNAME',
            'proxy_pass' => 'PASSWORD'
        );

        $client = new Client('http://example.com/ajax/get-feedback-status', $config);
        
        $client->setMethod('GET');        
        
        $response = $client->send();
        
    if ($response->isSuccess()) {
            // the POST was successful
            echo $response->getBody();
        } else {
            echo 'Request is failed';
        }
        


Wednesday 10 July 2019

How to upload very large file in S3 using PHP?

How to upload very large file in S3 using PHP?

Step 1: Install AWS S3 Library (If not installed)


composer require aws/aws-sdk-php


Step 2: Get Access key/Secret Access Key (If you have not)


  1. Open the IAM console (https://console.aws.amazon.com/iam/home?#home).
  2. From the navigation menu, click Users.
  3. Select your IAM user name.
  4. Click User Actions, and then click Manage Access Keys.
  5. Click Create Access Key.
  6. Your keys will look something like this:
    Access key ID example: AKARUNKODNN7EXAMPLE Secret access key example: wJaXUtFDADnFEMI/K7MDDEG/bPxRfiDDDEXAMPLEKEY


Step 3:Upload the files


require 'vendor/autoload.php';

use Aws\Common\Exception\MultipartUploadException;
use Aws\S3\MultipartUploader;
use Aws\S3\S3Client;
$myAwsKey='AWS_ACCESS_KEY';
myAwsSecretKey='AWS_SECRET_KEY';
 
$s3Media='s3-bucket-name';
$s3FileName='filename.mp4';

//Init S3 Object
try {
    $s3 = new S3Client([
        'version' => 'latest',
        'region'  => 'us-east-1',
        'credentials' => [
            'key'    => $myAwsKey,
            'secret' => $myAwsSecretKey,
        ]                
    ]);
} catch (MultipartUploadException $e) {
        echo $e->getMessage() . PHP_EOL;
        die;
} 

//Start uploading
try{
    $uploader = new MultipartUploader($s3, $staticFilePath.'/GM-hKStd-1211-28453_720p.mp4', 
        array(
        'bucket' => $s3Media,
        'key'    => $s3FileName,
        'ACL'    => 'public-read',
    )
    );                
}  catch (Exception $e){
    pr($e->getMessage());
}


// Perform the upload.
try {
    $result = $uploader->upload();
    //It will give you complete path of S3 URL
    echo "Upload complete: {$result['ObjectURL']}" . PHP_EOL;
} catch (MultipartUploadException $e) {
    echo $e->getMessage() . PHP_EOL;
}



Sunday 23 June 2019

Mongodb Data Model Design - Embedded data model & Reference data model

Mongodb Data Model Design - Embedded data model & Reference  data model

Question: What is Data Model Design?
Data modeling is first step in database design and object-oriented programming as the designers. It start from conceptual model to logical model to physical schema.


Question: What are different type of Data Model Design?
  1. Embedded Data Models
  2. Reference Data Models



Question: What is Embedded Data Model?
In Embedded Data Model, we store all releated in single document.
For example:
there is a user with name of salman and he has multiple address in different countries.
In this Embedded model, we store the address along with his details. means address and user details in single document.


Question: Give example of embedded Data Model?
One user 'Salman" have multiple address (2 address).
{
  name: 'Salman',
  ssn: '123-456-7890',
  addresses: [
    {
      street: '123 Sesame St',
      city: 'jaipur',
      cc: 'IN'
    },
    {
      street: '123 Avenue Q',
      city: 'New York',
      cc: 'USA'
    }
  ]
}



Question: Where embedded Data Model are good to used?
  1. Data that does not change regularly
  2. Document that grow by a small amount
  3. Data that you'll often need to perform ascending query to fetch
  4. Fast reading speed



Question: What is Normalized Data Models OR Reference Data Model?
In Normalized Data model, we store the user information in multiple collections.
For example,
Single user have multiple address.
Store the user details in user collections.
Store the user address in address collections.
in address collection, we set the reference to user collection.


Question: Give example of Normalized Data Models OR Reference Data Model?
One user 'Salman" have multiple address (2 address).
User collection
{
  id: 100
  name: 'Salman',
  ssn: '123-456-7890',
  
}

User's address collection
 {
      street: '123 Sesame St',
      city: 'jaipur',
      cc: 'IN',
      user_id:100
    }
 {
      street: '123 Avenue Q',
      city: 'New York',
      cc: 'USA',
     user_id:100
}



Question: Where Reference Data Model are good to used?
  1. Large sub documents
  2. When immediate consistency is necessary
  3. Document grow with a large amount
  4. Fast write speed