我有以下代码在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)值: [[城市: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), insert(com.mongodb.WriteConcern,[Lcom.mongodb.DBObject;), insert(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)
答案 0 :(得分: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'