试图使用mongodb插件在grails中创建一个mongodb集合 [英] Trying to create a mongodb collection in grails using mongodb plugin

查看:161
本文介绍了试图使用mongodb插件在grails中创建一个mongodb集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  BasicDBObject DBoptions = new BasicDBObject();我想在grails中创建一个mongodb集合。 
DBCollection collection = db.createCollection(xyz,DBoptions)

但是我得到以下错误:

 由GroovyRuntimeException引起:方法com.mongodb.DBApiLayer#createCollection的模糊方法重载。 
由于原型之间的重叠,无法解析为[class java.lang.String,class com.mongodb.BasicDBObject]
调用哪种方法:
[class java.lang.String,interface com .mongodb.DBObject]
[class java.lang.String,interface java.util.Map]

有关如何解决这个问题的任何想法?

p>在尝试从grails服务类运行时使用可用于mongo驱动程序的command()函数时,遇到了类似的问题。每当我尝试创建一个DBObject并将其传递给command()函数时,我会收到一个错误,抱怨com.mongodb.DBObject和java之间的重叠原型.util.interfaces。例如,尝试使用带有DB对象的db.command()创建数据库的新副本将引发该错误...

  DBObject cmd = new BasicDBObject()
cmd.put(copydb,1)
cmd.put(todb,to_database)
cmd.put(fromdb ,from_database)
mongo.getDB(admin)。command(cmd)

通过一些小路径/错误,实际上最终为我工作的并不是创建一个DBObject,而是通过了一个标准的groovy映射实例。我的猜测是,因为DBObject使用java.util.Map接口,所以两者应该大部分可以互换。下面的代码结束了我的工作。

  def cmd = [copydb:1,todb:to_database, fromdb:from_database] 
mongo.getDB(admin)。command(cmd)

听起来就像您找到了另一个解决方法,但是如果您将地图作为options参数传递给db.createCollection()而不是BasicDBObject,那么我会好奇的,如果它可以工作的话。



不管怎么说,希望这可以帮助那些可能正努力尝试让其他mongo api函数在grails中工作的人......


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

解决方案

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

这篇关于试图使用mongodb插件在grails中创建一个mongodb集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆