问题
I am trying to create a mongodb collection in grails using:
BasicDBObject DBoptions = new BasicDBObject();
DBCollection collection = db.createCollection("xyz",DBoptions)
But I get the following error:
Caused by GroovyRuntimeException: Ambiguous method overloading for method com.mongodb.DBApiLayer#createCollection.
Cannot resolve which method to invoke for [class java.lang.String, class com.mongodb.BasicDBObject]
due to overlapping prototypes between:
[class java.lang.String, interface com.mongodb.DBObject]
[class java.lang.String, interface java.util.Map]
Any ideas on how to solve this?
Thanks
回答1:
I came across a similar problem in trying to use the command() function available for the mongo driver when running from a grails service class.
Every time I tried to create a DBObject and pass that into the command() function I would get an error complaining about overlapping prototypes between the com.mongodb.DBObject and java.util.interfaces. For example trying to create a new copy of a database using db.command() with a DB object would throw that error...
DBObject cmd = new BasicDBObject()
cmd.put("copydb", 1)
cmd.put("todb", "to_database")
cmd.put("fromdb", "from_database")
mongo.getDB("admin").command(cmd)
Through a little trail/error what actually ended up working for me was instead of creating a DBObject, I passed in a standard groovy map instance. My guess is that since the DBObject uses the java.util.Map interface that the two should be interchangeable for the most part. The code below ends up working for me..
def cmd = [ copydb: 1, "todb": "to_database", "fromdb": "from_database" ]
mongo.getDB("admin").command(cmd)
Sounds like you found another work around, but I would be curious if you pass in a map as the options parameter to db.createCollection() instead of a BasicDBObject if it would work.
Anyways hope this helps someone out there who might be struggling trying to get some of the other mongo api functions to work inside grails...
来源:https://stackoverflow.com/questions/12748788/trying-to-create-a-mongodb-collection-in-grails-using-mongodb-plugin