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

a を使用するHashMap場合は、左シフト演算子を使用してドキュメントをコレクションに挿入します。

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

集計を使用している場合は、2 番目のアプローチを使用します。

サンプル

テストすると、これは私にとって完璧に機能します。

@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 に答える