2013-06-05 12 views
0

我有下面的代碼在Groovy中插入一個文檔,但我不斷收到在Grails應用程序無法使用gmongo

def zipcode = getDocumentCollection() 
    zipcode.insert(["city": "ACMAR", "loc": [-86.51557F, 33.584132F], "pop": 6055, "state": "AL", "_id": "35004"]) 

法無簽名這個錯誤插入蒙戈文件:com.mongodb.DBApiLayer $ MyCollection.insert() 適用於參數類型:(java.util.LinkedHashMap)值: [[city:ACMAR,loc:[ - 86.51557,33.584133],...]]可能的解決方案: insert([ Lcom.mongodb.DBObject;),insert(java.util.List), insert([Lcom.mongodb.DBObject; com.mongodb.WriteConcern), inse (com.mongodb.WriteConcern,[Lcom.mongodb.DBObject;), insert(java.util.List,com.mongodb.WriteConcern)

此代碼取自gmongo的示例。任何想法爲什麼我得到一個錯誤?

UPDATE
我得到的Grails應用程序下面的錯誤嘗試@ dmahapatro的做法後:

2013-06-06 09:54:21,493 [localhost-startStop-1] ERROR context.GrailsContextLoader - Error initializing the application: Error creating bean with name 'org.springframework.data.mongodb.monitor.OperationCounters#0': Unsatisfied dependency expressed through constructor argument with index 0 of type [com.mongodb.Mongo]: Could not convert constructor argument value of type [com.gmongo.GMongo] to required type [com.mongodb.Mongo]: Failed to convert value of type 'com.gmongo.GMongo' to required type 'com.mongodb.Mongo'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.gmongo.GMongo] to required type [com.mongodb.Mongo]: no matching editors or conversion strategy found 
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.data.mongodb.monitor.OperationCounters#0': Unsatisfied dependency expressed 
through constructor argument with index 0 of type [com.mongodb.Mongo]: Could not convert constructor argument value of type [com.gmongo.GMongo] to required type [com.mongodb.Mongo]: Failed to 
convert value of type 'com.gmongo.GMongo' to required type 'com.mongodb.Mongo'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.gmongo.GMongo] to required type [com.mongodb.Mongo]: no matching editors or conversion strategy found 
     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334) 
     at java.util.concurrent.FutureTask.run(FutureTask.java:166) 
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) 
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) 
     at java.lang.Thread.run(Thread.java:722) 
+0

你特別指的是哪個例子?什麼是'getDocumentCollection()'?闡述。 – dmahapatro

+0

聚合。 getDocumentCollection擴展爲mongo.getDB(「documents」)。getCollection(「zipcodes」) – allthenutsandbolts

+0

1.爲什麼當你有[MongoDB for Grails插件](http://grails.org/plugin/mongodb)時顯式使用GMongo驅動程序? ? 2.顯示完整的代碼(類似於我的回答),在Grails應用程序中使用GMongo。 3.刪除爲堆棧跟蹤添加的額外答案。我已經添加了相同的主要問題。 – dmahapatro

回答

0

當您使用insert您需要提供鍵 - 值對作爲命名ARGS(我想你也可以使用地圖,但不會太冗長)。

zipcode.insert(city: "ACMAR", loc: [-86.51557F, 33.584132F], pop: 6055, state: "AL", _id: "35004") 

如果你想使用HashMap然後使用向左移位運算符將文檔插入集合。

zipcode << ["city": "ACMAR", "loc": [-86.51557F, 33.584132F], "pop": 6055, "state": "AL", "_id": "35004"] 

我會用第二種方法,如果我使用聚合。

樣品

測試時,這工作非常適合我。

@Grab(group='com.gmongo', module='gmongo', version='1.0') 
import com.gmongo.GMongo 

def mongo = new GMongo("127.0.0.1", 27017) 
def db = mongo.getDB("gmongo") 
//Instead of doing below I can also use db.zipcodes.insert(blah: blah) 
def zipCode = db.getCollection("zipcodes") 
zipCode.insert(city: "ACMAR", loc: [-86.51557F, 33.584132F], pop: 6055, state: "AL", _id: "35004") 
zipCode << [city: "DUMMY", loc: [-86.51587F, 33.584172F], pop: 6056, state: "AL", _id: "35005"] 

assert db.zipcodes.findOne(city: "DUMMY").city == 'DUMMY' 
assert db.zipcodes.findOne(city: "ACMAR").city == 'ACMAR' 
+0

我得到了同樣的錯誤... – allthenutsandbolts

+0

@allthenutsandbolts看看我的示例代碼。測試Groovy 2.1.3 – dmahapatro

+0

代碼在獨立env中工作,但是當我嘗試在grails應用程序中執行時,出現錯誤 – allthenutsandbolts