unable to insert document in mongo using gmongo

1.1k Views Asked by At

I have the following code to insert a document in groovy but I keep getting this error in grails applications

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

No signature of method: com.mongodb.DBApiLayer$MyCollection.insert() is applicable for argument types: (java.util.LinkedHashMap) values: [[city:ACMAR, loc:[-86.51557, 33.584133], ...]] Possible solutions: 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)

this code is taken from the example of gmongo. Any ideas why I'm getting an error ?

UPDATE
I get the below error in Grails app after trying @dmahapatro's approach:

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

There are 1 best solutions below

2
On

When you use insert you need to provide the key-value pairs as named args (I think you can also use a map but it will be less verbose).

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

If you want to use a HashMap then use the left-shift operator to insert documents into collection.

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

I would go with the second approach if I were using aggregation.

Sample

This works perfect for me when tested.

@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'