Question

I'm having a hard time trying to "import" Mongodb's java driver into Groovy/Grails. In other words, what dependency gets added to the config files and where? At this point, I have the file "mongo-java-driver-2.12.1.jar" downloaded.

Was it helpful?

Solution

Did you look into using the grails mongo driver here? https://grails.org/plugins/mongodb. This plugin automatically adds the Mongodb java driver and the equivalent helper beans to grab db connections right in your servers. It also primarily adds GORM support. Add the following to your BuildConfig.groovy plugins block:

plugins {
  compile ":mongodb:3.0.1"
}

Also, depends on the version of grails you are using. This is recommended for grails 2.3.7 or above. However, if you are using anything2.3.7 or below you need to add this to your BuildConfig.groovy

dependencies {
  compile 'org.grails:grails-datastore-gorm:3.0.4.RELEASE'
  compile 'org.grails:grails-datastore-core:3.0.4.RELEASE'
  test 'org.grails:grails-datastore-simple:3.0.4.RELEASE'
}

OTHER TIPS

I have solved this issue. You don't actually import the jar or anything like that. What you do is that when you declare the Mongo GORM plugin dependency by putting this line

compile ':mongodb:3.0.1'

in your BuildConfig.groovy file, you actually, are importing the driver, after that its as simple as:

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

and now you can use the db variable to access the database.

Adding

compile 'org.mongodb:mongo-java-driver:2.13.1'

in Dependency section of BuildConfig.groovy worked for me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top