0

我有以下代码可以在 groovy 中插入文档,但我在 grails 应用程序中不断收到此错误

   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), insert(com.mongodb.DBObject, com .mongodb.WriteConcern),插入(com.mongodb.WriteConcern,[Lcom.mongodb.DBObject;),插入(java.util.List,com.mongodb.WriteConcern)

此代码取自 gmongo 的示例。任何想法为什么我会出错?

更新
在尝试@dmahapatro 的方法后,我在 Grails 应用程序中收到以下错误:

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

1 回答 1

0

当您使用时,insert您需要提供键值对作为命名参数(我认为您也可以使用映射,但它会不那么冗长)。

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

如果要使用 aHashMap然后使用左移运算符将文档插入到集合中。

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'
于 2013-06-05T17:05:00.127 回答