Tuesday, 27 September 2016

Mongo db

1- mongo db backup:-

//database backup and restore command

mongodump --db nexgtv_16 --out /root/mong_nexgtv_9aug16
mongorestore --db nexgtv_16 /home/mongodump/mong_nexgtv_9aug16/nexgtv_16

//mongo collection install command



http://www.usingscript.com/import-and-export-example-in-mongodb/


$ mongoexport
Export MongoDB data to CSV, TSV or JSON files.
options:
  -h [ --host ] arg         mongo host to connect to ( <set name>/s1,s2 for
  -u [ --username ] arg     username
  -p [ --password ] arg     password
  -d [ --db ] arg           database to use
  -c [ --collection ] arg   collection to use (some commands)
  -q [ --query ] arg        query filter, as a JSON string
  -o [ --out ] arg          output file; if not specified, stdout is used



$ mongoexport -d mydb -c mycollection -o mybackup.json

connected to: 127.0.0.1

exported 9438 records





Export all documents/recordswith some specific fields. Lets say only two fields "empName" and "empAddress" is to be exported


$ mongoexport -d mydb -c mycollection -f "empName,empAddress" -o mybackup2.json

connected to: 127.0.0.1

exported 4349 records





Restore database with mongoimport




$ mongoimport
connected to: 127.0.0.1
no collection specified<span class="token operator">!</span>
Import CSV, TSV or JSON data into MongoDB.
options:
  -h <span class="token punctuation">[</span> --host <span class="token punctuation">]</span> arg       mongo host to connect to <span class="token punctuation">(</span> <span class="token operator">&lt;</span>set name<span class="token operator">&gt;</span>/s1,s2 <span class="token keyword">for</span> sets<span class="token punctuation">)</span>
  -u <span class="token punctuation">[</span> --username <span class="token punctuation">]</span> arg   username
  -p <span class="token punctuation">[</span> --password <span class="token punctuation">]</span> arg   password
  -d <span class="token punctuation">[</span> --db <span class="token punctuation">]</span> arg         database to use
  -c <span class="token punctuation">[</span> --collection <span class="token punctuation">]</span> arg collection to use <span class="token punctuation">(</span>some commands<span class="token punctuation">)</span>
  -f <span class="token punctuation">[</span> --fields <span class="token punctuation">]</span> arg     comma separated list of field names e.g. -f name,age
  --file arg              <span class="token function">file</span> to <span class="token function">import</span> from<span class="token punctuation">;</span> <span class="token keyword">if</span> not specified stdin is used
  --drop                  drop collection first
  --upsert                insert or update objects that already exist








$ mongoimport -d mydb -c mycollection --file mybackup.json

connected to: 127.0.0.1

Sat Jan 16 12:06:10 imported 19878 objects





Imports all documents/records, insert missing records and update existing records based on _id ( mongoId)


$ mongoimport -d mydb -c mycollection --file mybackup.json --upsert

connected to: 127.0.0.1

Sat Jan 16 13:20:02 imported 11102 objects





Imports all records/documents to remote MongoDB server like  mongolab.com, using username and password, from local file mybackup.json


$ mongoimport -h id.mongolab.com:66078 -d mydb -c mycollection -u myusername -p mypassword --file mybackup.json

connected to: id.mongolab.com:66078

Sat Jan 16 14:21:16 imported 16603 objects




2- db.copyDatabase('nexgtv_16','nexgtv_bcup_9aug','172.31.22.235');


3-  Create replicate table in mongodb

db.ASSETS.aggregate([{"$match":{}},{"$out":"Assets_5August"}])


4. //group by record

db.ASSETS.aggregate(
   {$group : { '_id' : "$channel_name",'charge_code':{'$first':'$charge_code'},'platform':{'$first':'$platform'}}}
)


 db.KEYWORDS.aggregate(
 [
 {"$group":
 {"_id":{"title":"$title"},
 "uniqueIds":{"$addToSet":"$_id"},
 "count":{"$sum":1}}},
 {"$match":{"count":{"$gte":2}}},
 {"$sort":{"count":-1}}
 ]).pretty();


 db.KEYWORDS.aggregate(

 {"$group":
 {"_id":{"title":"$title"},
 "uniqueIds":{"$addToSet":"$_id"},
 }}


 ).pretty();


===============================================================

Mongo dump

1:- create test.sh file write given command then after set in crone


#!/bin/bash

MONGO_DATABASE="nexgtv_16"
APP_NAME="nexgtv"
MONGO_HOST="127.0.0.1"
MONGO_PORT="27017"
TIMESTAMP=`date +%F-%H%M`
MONGODUMP_PATH="/usr/bin/mongodump"
BACKUPS_DIR="/home/appinvn/backups/$APP_NAME"
BACKUP_NAME="$APP_NAME-$TIMESTAMP"
$MONGODUMP_PATH -d $MONGO_DATABASE
mkdir -p $BACKUPS_DIR
mv dump $BACKUP_NAME
tar -zcvf $BACKUPS_DIR/$BACKUP_NAME.tgz $BACKUP_NAME
rm -rf $BACKUP_NAME

find /home/appinvn/backups/nexgtv/ -mindepth 1 -mtime +7 -delete



==================================================================================

mongo db mail file


<?php  
//require_once('/var/www/html/live/web/PHPMailer/class.phpmailer.php');

require_once('/var/www/html/live/web/PHPMailer/PHPMailerAutoload.php');
set_time_limit(0);
ob_start();
date_default_timezone_set("Asia/Kolkata"); 
//$mongo = "172.31.22.236:27017";
//$conn = new MongoClient("mongodb://$mongo");
$conn = new MongoClient("mongodb://Appinv-db:27017,Appinv-db1:27017,Appinv-db2:27017", array("replicaSet" => "rs0"));
if(!$conn){
die("Unable to connect with mongodb");
}

$db = $conn->nexgtv_16;


$USED_VOUCHERS = $db->USED_VOUCHERS;

  $oldDate = strtotime("now") - 24*60*60*7; 
 //  $oldDate = strtotime("now") - 24*60*60*90; 


$timecomapre = array("created_time"=>array('$gte'=>$oldDate));
$records = $USED_VOUCHERS->find($timecomapre);

$VOUCHERSRec = $db->VOUCHERS;
$vouRecfind = $VOUCHERSRec->find();

$voucherArray = array();
foreach($records as $val){
$vids = explode(',',$val['voucherIds']);
foreach($vids as $v){
@$voucherArray[$v] += 1;
}
}

foreach($vouRecfind as $vouval){
$vouchId=(string)$vouval['_id'];
if(!isset($voucherArray[$vouchId]))
$voucherArray[$vouchId] = 0;
}

// echo "<pre>";
// print_r($voucherArray); 

// exit;
//$VOUCHERSRec = $db->VOUCHERS;

?><?php
$html_content =" Dear Team, <br /> <br />   Please look total number of voucher codes count for the last 7 day.<br /> <br /> ";

$html_content .='<table border="1" cellpadding="0" cellspacing="0">



<tr>
<td><b>Brand Name</b></td>

<td><b>Number of vouchers used</b></td>


</tr>';
?>
<?php

foreach ($voucherArray as $key=>$value){


$records = $VOUCHERSRec->findOne(array('_id' => new MongoId($key)));

//echo $records["brand_name"]."==========".$value;
//echo "<pre>";

//echo date('Y-m-d H:i:s', $records['voucher_update_time']->sec);

// echo $records['validity_date']->sec.' '.$records['brand_name'].' '.'<br>';

/* if($records['validity_date']->sec < strtotime('now') && $records['brand_name'] == "Grofers" ){

$records['brand_name'] = "Grofers old (".date('Y-m-d H:i:s', $records['voucher_insert_time']->sec).")" ;

}else if($records['brand_name'] == "Grofers" ){
$records['brand_name'] = "Grofers ";


*/


//$datecheck = array("created_time"=>array('$lte'=>$records['voucher_update_time']->sec));



$html_content .= '<tr>


<td> '. $records['brand_name'] ."&nbsp;(".date('d-m-Y', $records['validity_date']->sec).')</td>

<td>'. $value .'</td>


</tr>';


 } 



$html_content .= '</table>';

$html_content .="<br /> <br />  Regards, <br />  nexGTv Development Team";

echo $html_content;






// ****************MAIL SEND******************* 



$mail = new PHPMailer();  

                $mail->IsSMTP();  // telling the class to use SMTP
                $mail->Mailer = "smtp";
                $mail->Host = "172.31.20.133";
//   $mail->Host = "172.32.20.133";
                $mail->Port = 25;
                $mail->SMTPAuth = false; // turn on SMTP authentication
                //$mail->Username = "ashish.rawat@infotelgroup.in"; // SMTP username
                //$mail->Password = "yourpassword"; // SMTP password 
                $mail->IsHTML(true);  
                $mail->From     = "digivive_analytics@admin.com"; 
//meha.saluja@digivive.com
//$mail->AddAddress("navdeep.aswal@digivive.com"); 
$mail->AddAddress("meha.saluja@digivive.com"); 
$mail->AddAddress("tapan.thapa@digivive.com"); 
  $mail->AddAddress("sharma.pankaj@digivive.com"); 
$mail->AddAddress("manoj.tiwari@digivive.com"); 



$mail->Subject  = "weekly voucher report ";


//$htmlBody =" Dear Team, <br /> <br />   Please look total number of voucher codes count for the last 7 day.<br /> <br /> <br />  Regards, <br />  nexGTv Development Team";


$mail->Body     = $html_content;
$mail->WordWrap = 50;  

//$mail->AddAttachment("phpmailer.png");
//$mail->AddAttachment($filename);



if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}







?>





mongodb installation
https://ask.openstack.org/en/question/80986/not-able-to-start-mongd-service/
https://ask.openstack.org/en/question/54069/rdo-installation-problem-on-centos-7-because-of-mongodb/
[root@network ~]# yum --enablerepo=epel -y install mongodb-server mongodb
[2] Configure and Start MongoDB.
[root@network ~]# vi /etc/sysconfig/mongod
# add
OPTIONS="--smallfiles --quiet -f /etc/mongodb.conf"
[root@network ~]# vi /etc/mongod.conf
# line 6: change to the own IP
bind_ip = 10.0.0.50
[root@network ~]# systemctl start mongod 
[root@network ~]# systemctl enable mongod
=========================================================================
Q1:- update query command line using mongo db:-

db.ASSETS.update({categories:{$in:['CTG513','CTG514']}},{$set: {content_id:"5682cd15cc95b319a65da89b"}},{multi:true})
Q2:-
db.ASSETS.aggregate([{"$match":{"plateform":{"$in":["mobile_app","all"]},"status":"1","catlogue":{"$in":["565c8c878c86ba151d29b689"]},
  "type":"movie","$or":[{"content_id":{"$in":["5682cbe8cc95b319a65da88f"]}},{"is_pack_assigned":"1"}],
  "priority_menu.565c8c878c86ba151d29b689.default":{"$exists":true}}},{"$sort":{"priority_menu.565c8c878c86ba151d29b689.default":-1}},
  {"$skip":10},{"$limit":10}])
Q3:-
//mongodb installation centos
https://www.howtoforge.com/tutorial/how-to-install-and-configure-mongodb-on-centos-7/