Java Script Expressions in MongoDB Querying

Basically, MongoDB is an Object-Oriented Database where we not only use MongoDB Query to fetch the results, we can also use this JavaScript like expressions to operate the data from database. We can use some basic operations like ‘&&’, ‘>’, ‘!=’, etc. below is the example for both JavaScript and MongoDB Querying.

Example: trying to get the results with style number greater than 600000 and style description not equal to MM GRAPHIC PO HOOD.

Query using JavaScript expression:

db.styles.find(
"this.style.number > 600000 && this.style.description !='MM GRAPHIC PO HOOD'"

)


Query using MongoDB Syntax:

db.style.find(
{
"style.number":{$gt:"600000"},
"style.description":{$ne:"MM GRAPHIC PO HOOD"}}
)



Performance matters:

                 To get the best performance, use the Mongo Query rather than javaScript expression to fetch the results.



Read More »

Types of Exceptions and how to handle them in C# .Net

Any human cannot write 100% complete bug free code, sometimes users might give unrealistic data to the application, that might cause exceptions, some of the types of exceptions that should be commonly handled in .net are :


Example for NullReferenceException


        public static void handleNullException()
        {
            try
            {
                string name = null;
                int nameLength = name.Length;
            }
            catch (NullReferenceException ne)
            {
                Console.WriteLine("Exception : " + ne.Message);
            }
        }



Example for DivideByZeroException


        public static void handleDivideByZero()
        {
            try
            {
                int result = 20 / int.Parse("0");
            }
            catch (DivideByZeroException de)
            {
                Console.WriteLine("Exception : " + de.Message);
            }
        }


Example for FormatException 


        public static void handleFormatException()
        {
            try
            {
                int number = int.Parse("string");
            }
            catch (FormatException ce)
            {
                Console.WriteLine("Exception : " + ce.Message);
            }
        }


Example for InvalidCastException


        public static void handleCastException()
        {
            try
            {
                int a = 10;
                object data = "name";
                int b = (short)data;
            }
            catch (InvalidCastException ce)
            {
                Console.WriteLine("Exception : " + ce.Message);
            }
        }


Example for IndexOutOfRangeException


   public static void handleIndexOutofRange()
        {
            try
            {
                int[] series = { 2, 8, };

                for (int i = 0; i < 2; i++)
                {
                    int aa = series[i + 3];
                }
            }
            catch (IndexOutOfRangeException ex)
            {
                Console.WriteLine("Exception : " + ex.Message);
            }
        }


Handling Multiple Exceptions in C#


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExecptionProj
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //functionlity here.
            }
            catch (InvalidCastException ce)
            {
                Console.WriteLine("functionality here");
            }
            catch (NullReferenceException nre)
            {
                Console.WriteLine("functionality here");
            }
            catch (IndexOutOfRangeException iore)
            {
                Console.WriteLine("functionality here");
            }
            catch (DivideByZeroException dbz)
            {
                Console.WriteLine("functionality here");
            }
            catch (OutOfMemoryException om)
            {
                Console.WriteLine("functionality here");
            }
        }
    }
}



Read More »