How to execute mongo commands through shell

Lets say you need to find out which is the primary node in the mongo cluster in your shell script,

mongo –host 10.126.71.162 –username=userid –password=secret –eval “db.runCommand(‘ismaster’).primary”

This would quickly open the mongo shell and execute the commands, and give the output as the last line

MongoDB shell version v4.0.5
connecting to: mongodb://10.126.71.162:27017/?gssapiServiceName=mongodb
Implicit session: session { “id” : UUID(“65f2e5ab-aa97-4306-8a20-afd70a4ceeb0”) }
MongoDB server version: 4.0.5
10.126.71.162:27017

Certificate: Shell Scripting: Discover How to Automate Command Line Tasks

This certificate above verifies that Aseem Jain successfully completed the course Shell Scripting: Discover How to Automate Command Line Tasks on 11/05/2019 as taught by Jason Cannon on Udemy. The certificate indicates the entire course was completed as validated by the student.

How to install tree command in Mac

##Adding Tree For the sake of simplicity, I chose to use an alias for the tree command rather than creating a new directory and altering my function search path to read that directory. I searched the web for a bit and found a solution that worked perfectly. Simply open up your .zshrc file and add this code:

alias tree="find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'"

now return to the command line and run:

source .zshrc

For those who are unaware, the tree function is a useful shell command that displays the file structure of a given directory in a visual manner. For example, if I am in a directory titled my_blog and within that directory have three folders html, css and js, tree would show something like this:

|____mysqlrouter_passwd
|____Notifier.app
| |____Contents
| | |_____CodeSignature
| | | |____CodeResources
| | |____MacOS
| | | |____Notifier
| | |____Resources
| | | |____en.lproj
| | | | |____MainMenu.nib
| | | | |____InfoPlist.strings
| | | | |____Credits.rtf
| | | |____AppIcon.icns
| | | |____Assets.car
| | |____Info.plist
| | |____PkgInfo

Certificate: Grafana and Graphite course completion

This certificate above verifies that Aseem Jain successfully completed the course Grafana and Graphite from Beginner to Advanced (3rd Edition) on 10/20/2019 as taught by Aref K. on Udemy. The certificate indicates the entire course was completed as validated by the student.

Binary Search Tree

package me.premaseem.datastructure.binarySearchTree;


class Node {
    Node left, right;
    int data;

    public Node(int d) {
        data = d;
    }

}

public class BinarySearchTree {

    Node root;
    // Driver Program to test above functions
    public static void main(String[] args) {
        BinarySearchTree tree = new BinarySearchTree();

        /* Let us create following BST
              50
           /     \
          30      70
         /  \    /  \
       20   40  60   80 */

        tree.insert(50);
        tree.insert(30);
        tree.insert(20);
        tree.insert(40);
        tree.insert(70);
        tree.insert(60);
        tree.insert(80);

//      print inorder traversal of the BST
        tree.inOrder();
        
        tree.search(100);
        tree.search(30);
        
        tree.delete(30);
        tree.delete(30);
        tree.delete(50);
        
        System.out.println();
        tree.inOrder();
    }

    private void delete(int d) {
        root = deleteR(root,d);
    }

    private Node deleteR(Node n ,int d) {
        if(n == null){
            return n;
        }
        if(d < n.data){
            n.left = deleteR(n.left,d);
        }else if(d > n.data){
            n.right = deleteR(n.right,d);
        } else {
            // 1 or all nodes are null
            if(n.left == null){
                return n.right;
            }else if (n.right == null){
                return n.left;
            }

            n.data = findMin(n.right);
            n.right = deleteR(n.right,n.data);
            // if both node are not null
        }
        return n;

    }

    int findMin(Node n){
        int min = n.data;
        if(n.left != null){
            min = n.data;
            n = n.left;
        }
        return min;
    }


    private void insert(int d) {
        root = insertR(root,d);
    }

    private Node insertR(Node n ,int d) {
        if(n == null){
            return new Node(d);
        }
        if(d < n.data){
            n.left = insertR(n.left, d);
        }else {
            n.right = insertR(n.right, d);
        }
        return n;
    }

    void inOrder(){
        inOrderR(root);
    }

    private void inOrderR(Node n) {
        if(n == null){
            return;
        }
        inOrderR(n.left);
        System.out.print(" >> "+ n.data);
        inOrderR(n.right);
    }

    void search(int d){
        searchR(root, d);
    }

    private void searchR(Node n, int d) {
        if(n == null){
            System.out.println("not found ");
            return;
        }

        if(d == n.data){
            System.out.println("Element found 🙂 ");
            return;
        }
        if(d < n.data){
            searchR(n.left, d);
        }else {
            searchR(n.right,d);
        }

    }
}

Github Code Repository for coding challenge: 
https://github.com/premaseem/AlgoAndDataStructureInJava/blob/master/src/me/premaseem/datastructure/binarySearchTree/BinarySearchTree.java

Youtube playlist for more video like this:


## Follow me on
LinkedIn: http://in.linkedin.com/in/premaseem
Facebook: https://www.facebook.com/premaseem
Twitter:  https://twitter.com/premaseem
My Blog: https://premaseem.wordpress.com