Question

Do I have to have a domain object to query mongodb?

What if I just want some raw data to be displayed? What would be the syntax to query mongodb from my controller?

I tried

"def var = db.nameOfMyCollection.find()"

but it says no such property as db in my controller class.

I know that my application is connecting to the database because I am monitoring mongo server log and it increases the number of connections by one when I launch my grails app.

Was it helpful?

Solution

Assuming you have added mongodb java driver dependency in build config and refreshed your dependencies.

Create a grails service named MongoService.groovy and put the following code.

Dont forget to import mongodb

package com.organisation.project

import com.mongodb.*


class MongoService {
    private static MongoClient mongoClient
    private static host = "localhost"    //your host name
    private static port = 27017      //your port no.
    private static databaseName = "your-mongo-db-name"

    public static MongoClient client() {
        if(mongoClient == null){
            return new MongoClient(host,port)
        }else {
            return mongoClient
        }
    }

    public DBCollection collection(collectionName) {
        DB db = client().getDB(databaseName)
        return db.getCollection(collectionName)
    }
}

We can now use this MongoService in our controllers or other services.

Now you can do following stuff in your controller.

Dont forget to import mongodb.DBCursor

package com.organisation.project



import com.mongodb.DBCursor

class YourControllerOrService {

    def mongoService    //including Mongo service 

    def method(){
        def collection = mongoService.collection("your-collection-name")
        DBCursor cursor =  collection.find()
        try{
            while(cursor.hasNext()){
                def doc = cursor.next()
                println doc     //will print raw data if its in your database for that collection
                }

         }finally {
                  cursor.close()
         }

    }
}

For more info Refer mongodb java docs

OTHER TIPS

Ok, solved.

This is how you go about accessing the database.

import com.mongodb.*
MongoClient mongoClient = new MongoClient("localhost", 27017)
        DB db = mongoClient.getDB("db");

I actually solved it using Java and then pasted it into groovy and it works there as well which shouldn't come as a surprise. The difference is that in Java you actually have to import the jar driver, but in Grails, you install the Mongo GORM plugin.

Assuming you are using the MongoDB GORM Plugin, if you have domain classes in your grails application, you can use them as you would with any relational db backend.

However, per this documentation, you can access the low-level Mongo API in any controller or service by first declaring a property mongo, just as you would a service, then getting the database you are targeting:

def mongo
def myAction = {
    def db = mongo.getDB("mongo")
    db.languages.insert([name: 'Groovy'])
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top