首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用gmongo在mongo中插入文档

无法使用gmongo在mongo中插入文档
EN

Stack Overflow用户
提问于 2013-06-05 15:17:23
回答 1查看 1.2K关注 0票数 0

我有下面的代码可以在groovy中插入一个文档,但是我在grails应用程序中一直有这个错误。

代码语言:javascript
复制
   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,.]]可能的解决方案:插入( [Lcom.mongodb.DBObject;),插入(java.util.List),插入(Lcom.mongodb.DBObject;,com.mongodb.WriteConcern),插入(com.mongodb.DBObject,com.mongodb.WriteConcern),插入(com.mongodb.WriteConcern,[Lcom.mongodb.DBObject;),插入(java.util.List,com.mongodb.WriteConcern) )

这段代码取自gmongo的例子。知道我为什么会出错吗?

更新

在尝试@dmahapatro的方法之后,我在Grails应用程序中得到了以下错误:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2013-06-05 17:05:00

当您使用insert时,您需要提供名为args的键值对(我认为您也可以使用一个映射,但它不会太冗长)。

代码语言:javascript
复制
zipcode.insert(city: "ACMAR", loc: [-86.51557F, 33.584132F], pop: 6055, state: "AL", _id: "35004")

如果要使用HashMap,则使用左移位操作符将文档插入集合中。

代码语言:javascript
复制
zipcode << ["city": "ACMAR", "loc": [-86.51557F, 33.584132F], "pop": 6055, "state": "AL", "_id": "35004"]

如果我使用聚合,我会使用第二种方法。

样本

当测试时,这对我来说是完美的。

代码语言:javascript
复制
@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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16943655

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档