且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

试图使用mongodb插件在grails中创建一个mongodb集合

更新时间:2023-02-14 19:22:38

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...