Using MongoDB Low Level Query In Grails CRUD Opereation

Posted By : Amit Kumar | 28-Jul-2014

Using MongoDB Low Level Query In Grails | CRUD Opereation

I have encountered, performance issue in my recent project where we query for large data from database , since it was affecting application performance in many areas. So i decided to use Low Level MongoDB query through grails. Earlier i was using GORM to fetch the data which is not good good in this case.

 

The main advantage of using MongoDB Low Level Query in Grails :

 

1.) It uses native MongoDB query

2.) It returns BSON type result, which is superset of JSON and can be easily evaluated.

3.) MongoDB Native Query is many times faster than GORM Query with MongoDB.

4.) It also provide aggregation framework to deal with complex data structure

 

 

Note :

 

Grails Version : 2.3.6

MongoDB Version : mongodb:2.0.1

 

 

In this tutorial we will have look on CRUD operation through Grails MongoDB Native Query. I will use Employee Domain as example.

 

Code Snippet :- Employee.groovy

 

class Employee{
	String name
	String address
}

Create :

It is simillar to MongoDB query insert.If given collection is not exists will create it with new document. To execute MongoDB native query we use collection method on Domain class followed by one of the mongodb CRUD methods/functions.

Code Snippet :-

def doc1=['name':'ABC1',address:'XYZ1']
def doc2=['name':'ABC2',address:'XYZ2']
def doc3=['name':'ABC3',address:'XYZ3']
def doc4=['name':'ABC1',address:'XYZ4']
def doc5=['name':'ABC5',address:'XYZ5']
Employee.collection.insert(doc1)
Employee.collection.insert(doc2)
Employee.collection.insert(doc3)
Employee.collection.insert(doc4)
Employee.collection.insert(doc5)
 

Update :

It is simillar to MongoDB query update.Method has three parameter, first is where clause and second is what is to be set and third is optional.The below query will update 'address' field in all documents where name is “ABC1”

Code Snippet :-

	def where=['name':'ABC1']
	def update=['$set':['address':'EFG']]
	def option=[multi:1]
	Employee.collection.update(where,update,option)
 

Remove :

It is simillar to MongoDB query remove.Method has two parameter, first is where clause and second is optional.The below query will delete all documents where name is “ABC1”

	def where=['name':'ABC1']
	def option=[multi:1]
	Employee.collection.remove(where,option)
 

Find :

It is simillar to MongoDB query find.Method has two parameter, first is where clause and second is optional, this parameter will decide which fields will be visible to the output results, by deafult id visibles. The below query will find all documents where name is “ABC1”. We can use toArray() method on returned results to get array list

Code Snippet :-

	def where=['name':'ABC1']
	def option=[_id:0,address:1]
	def results=Employee.collection.find(where,option)
	def outputArray=results .toArray()

 

Hope it will help you.

Thanks

Amit

 

 

About Author

Author Image
Amit Kumar

Amit is a bright Groovy and Grails developer and has worked on development of various SaaS applications using Grails framework.

Request for Proposal

Name is required

Comment is required

Sending message..