January 15, 2017

How to use like keyword in mongodb

Like SQL statements we can use LIKE keywords in Mongo as well. We can query mongdb with LIKE search. We can restrict LIKE search as we do in SQL using '%' as

db.employee.find({name: /text/})  //like '%text%'
db.employee.find({name: /^txt/})  //like 'text%'
db.employee.find({name: /text$/})  //like 'text%'
Examples:

Employee Collection
 {   
   "_id" : 1,   
   "name" : {   
    "first" : "Abhi",   
    "last" : "Dev"   
   },   
   "department" : "finance",   
   "joineddate" : "2000-04-10"      
  },   
  {   
   "_id" : 2,   
   "name" : {   
    "first" : "Alok",   
    "last" : "Agrawal"   
   },   
   "department" : "information",   
   "joineddate" : "2006-07-07"      
  },   
  {   
   "_id" : 3,   
   "name" : {   
    "first" : "Dhruv",   
    "last" : "Sharma"   
   },   
   "department" : "finance",   
   "joineddate" : "2010-09-07"      
  }   

Lets say from above collection I need to search for name which has 'h' in it. So, the query would be:

  db.employee.find({"name" : "/h/"},{name:1})  

The above will fetch 2 results from collection as:
  {    
   "_id" : 1,    
   "name" : {    
   "first" : "Abhi",    
   "last" : "Dev"    
   }  
  },  
  {    
   "_id" : 3,    
   "name" : {    
   "first" : "Dhruv",    
   "last" : "Sharma"    
   }   
  }  

Now, lets say I need to search name that start with 'A', so the query would be:
  db.employee.find({"name" : "/^A/"},{name:1})  

Query Output:
  {    
   "_id" : 1,    
   "name" : {    
   "first" : "Abhi",    
   "last" : "Dev"    
   }   
  },  
  {    
   "_id" : 2,    
   "name" : {    
   "first" : "Alok",    
   "last" : "Agrawal"    
   }   
  }   

There are many regex we can use similarly to filter the search as per the need.

December 20, 2016

Execute MongoDB queries from command prompt

Execute MongoDB queries from command prompt

we can run mongodb queries from command prompt as well. Run command prompt in administrator mode.

Go to the mongo installation path till bin folder.

In windows, it should be like:

 c:\Program Files\Mongo\server\3.2\bin  

Use below command to execute the mongo query :

 mongo <hostname>:<portnumber>/<dbname> -u <user> -p <pass> db.employee.find("department" : "finance")  

The above command will connect to the given DB details and execute the query.

It will show the data in command prompt itself.

How to print specific data from Collection or document using Javascript in MongoDB

How to print specific data from Collection or document using Javascript in MongoDB

Mongo supports javascript queries to handle the data and process as per the need.

For example,

Employee collection
 {  
   "_id" : 1,  
   "name" : {  
     "first" : "Abhi",  
     "last" : "Dev"  
   },  
   "department" : "finance",  
   "joineddate" : "2000-04-10"       
 },  
 {  
   "_id" : 2,  
   "name" : {  
     "first" : "Alok",  
     "last" : "Agrawal"  
   },  
   "department" : "information",  
   "joineddate" : "2006-07-07"       
 },  
 {  
   "_id" : 3,  
   "name" : {  
     "first" : "Dhruv",  
     "last" : "Sharma"  
   },  
   "department" : "finance",  
   "joineddate" : "2010-09-07"       
 }  

Now, lets say I need to print only the name who are in finance department.

 db.employee.find("department" : "finance").forEach(     
   function(doc) {   
    print(doc.name.first + " " + doc.name.last);   
   );  

The above query will print all the employee first name and last name whose department is finance.