Rakesh A R
Rakesh A R

Reputation: 206

Groovy program to connect mongo db

I am trying to connect mongodb using Groovy language but i am getting error like this (which is posted below) and i have added necessary jar file.I am using mongodb verion : mongodb-driver-3.2.2.jar . Please help me to solve this problem

Exception in thread "main" java.lang.NoClassDefFoundError: com/mongodb/operation/OperationExecutor
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
    at java.lang.Class.getDeclaredConstructors(Unknown Source)
    at org.codehaus.groovy.reflection.CachedClass$2$1.run(CachedClass.java:69)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.codehaus.groovy.reflection.CachedClass$2.initValue(CachedClass.java:67)
    at org.codehaus.groovy.reflection.CachedClass$2.initValue(CachedClass.java:64)
    at org.codehaus.groovy.util.LazyReference.getLocked(LazyReference.java:46)
    at org.codehaus.groovy.util.LazyReference.get(LazyReference.java:33)
    at org.codehaus.groovy.reflection.CachedClass.getConstructors(CachedClass.java:258)
    at groovy.lang.MetaClassImpl.<init>(MetaClassImpl.java:213)
    at groovy.lang.MetaClassImpl.<init>(MetaClassImpl.java:223)
    at groovy.lang.MetaClassRegistry$MetaClassCreationHandle.createNormalMetaClass(MetaClassRegistry.java:168)
    at groovy.lang.MetaClassRegistry$MetaClassCreationHandle.createWithCustomLookup(MetaClassRegistry.java:158)
    at groovy.lang.MetaClassRegistry$MetaClassCreationHandle.create(MetaClassRegistry.java:141)
    at org.codehaus.groovy.reflection.ClassInfo.getMetaClassUnderLock(ClassInfo.java:209)
    at org.codehaus.groovy.reflection.ClassInfo.getMetaClass(ClassInfo.java:241)
    at org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl.getMetaClass(MetaClassRegistryImpl.java:255)
    at org.codehaus.groovy.runtime.InvokerHelper.getMetaClass(InvokerHelper.java:859)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.createCallConstructorSite(CallSiteArray.java:84)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:57)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:182)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:194)
    at com.sample.MongoService.client(MongoService.groovy:14)
    at com.sample.MongoService.collection(MongoService.groovy:21)
    at com.sample.MongoService$collection.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
    at com.sample.MongoDBController.method(MongoDBController.groovy:10)
    at com.sample.MongoDBController$method.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)
    at com.sample.GroovyDemoApp.main(GroovyDemoApp.groovy:12)
Caused by: java.lang.ClassNotFoundException: com.mongodb.operation.OperationExecutor
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    ... 35 more

Update :

This is my Groovy code. with this code i am trying to connect Mongodb

// error is in this line . i .e - 
// The type com.mongodb.operation.CreateIndexesOperation cannot be resolved.
// It is indirectly referenced from required .class files )
package com.sample

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 = "db"

  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)
  }
}

Upvotes: 2

Views: 12077

Answers (1)

Emmanuel Rosa
Emmanuel Rosa

Reputation: 9885

To use the MongoDB driver you can either use the JAR, or @Grab it from Maven.

Using the JAR

To use the JAR, you need to add it to Groovy's classpath. This is done with the -cp argument:

#!/usr/bin/env groovy -cp /path/to/jar/file

println 'Hello'

Using Maven

You can simply use Groovy's @Grab to take care of the dependency for you:

@Grab('org.mongodb:mongodb-driver:3.2.2')

println 'Hello'

Working example

Here's a working example based on the code you posted:

@Grab('org.mongodb:mongodb-driver:3.2.2')

import com.mongodb.MongoClient
import com.mongodb.DBCollection
import com.mongodb.DB
import com.mongodb.BasicDBObject

class MongoService { 
    private MongoClient mongoClient 

    def host = "localhost" //your host name 
    def port = 27017 //your port no. 
    def databaseName = 'test'

    public MongoClient client() {
        mongoClient = mongoClient ?: new MongoClient(host, port) 

        return mongoClient
    } 

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

        return db.getCollection(collectionName) 
    }
}

def service = new MongoService(databaseName: 'db')
def foo = service.collection('foo')

def data = [
    [firstName: 'Jane', lastName: 'Doe'],
    [firstName: 'Elvis', lastName: 'Presley']
].collect { it as BasicDBObject }

foo.insert(data)

foo.find().toArray().each {
    println it
}

I've never used MongoDB before, so whether my use case is useful or not is debatable.

Upvotes: 2

Related Questions