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
    }
}