gpt4 book ai didi

mongodb - 从Grails更新MongoDB文档

转载 作者:行者123 更新时间:2023-12-02 15:27:54 25 4
gpt4 key购买 nike

我试图基于某些属性更新mongodb记录。

我的收藏如下:

 db.website.findOne()
{
"_id" : NumberLong(91),
"crawled" : true,
"user" : NumberLong(50),
"version" : 0,
"website" : "http://www.google.com/"
}

我正在尝试从此集合中查找有关用户和网站的特定值的记录,然后尝试将已爬网的值从false更新为true。我尝试了以下操作,但不起作用。
class ResourceController implements Serializable{
def mongo = new GMongo()
def db = mongo.getDB("cmsProfiler")
static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]
def crawl(Integer max) {
def website = params.websiteSelection
String userWebsite = website
String loggedUser = session.user
def p = Website.findByWebsiteAndUser(website, session.user)
try{
if(p['crawled']==true){
render view:"/website/show", model:[message: "Already Crawled"]
return
}
else {
Website.collection.update( [ website:userWebsite, user:loggedUser],
[ $set:[ crawled:true ] ] )
}
}catch(Exception e)
{
System.out.println("Got an Exception: " + e.printStackTrace());
}

}

我收到错误: can't serialize class Website完整的堆栈跟踪如下:
   java.lang.IllegalArgumentException: can't serialize class cmsprofiler.User
Error |
at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:284)
Error |
at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:185)
Error |
at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:131)
Error |
at com.mongodb.DefaultDBEncoder.writeObject(DefaultDBEncoder.java:33)
Error |
at com.mongodb.OutMessage.putObject(OutMessage.java:289)
Error |
at com.mongodb.OutMessage.writeUpdate(OutMessage.java:179)
Error |
at com.mongodb.OutMessage.update(OutMessage.java:60)
Error |
at com.mongodb.DBCollectionImpl.update(DBCollectionImpl.java:269)
Error |
at com.mongodb.DBCollection.update(DBCollection.java:191)
Error |
at com.mongodb.DBCollection.update(DBCollection.java:224)
Error |
at com.mongodb.DBCollection.update(DBCollection.java:239)
Error |
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Error |
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
Error |
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
Error |
at java.lang.reflect.Method.invoke(Method.java:606)
Error |
at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1260)
Error |
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
Error |
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
Error |
at groovy.lang.MetaMethod$doMethodInvoke$0.call(Unknown Source)
Error |
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
Error |
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
Error |
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)
Error |
at com.gmongo.internal.Patcher._invoke(Patcher.groovy:49)
Error |
at com.gmongo.internal.Patcher$_invoke$5.callStatic(Unknown Source)
Error |
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:53)
Error |
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:157)
Error |
at com.gmongo.internal.Patcher$__patchInternal_closure1.doCall(Patcher.groovy:38)
Error |
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Error |
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
Error |
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
Error |
at java.lang.reflect.Method.invoke(Method.java:606)
Error |
at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1260)
Error |
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
Error |
at org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod.invoke(ClosureMetaMethod.java:80)
Error |
at groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:1108)
Error |
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:910)
Error |
at org.codehaus.groovy.runtime.callsite.PerInstancePojoMetaClassSite.call(PerInstancePojoMetaClassSite.java:35)
Error |
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
Error |
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
Error |
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)
Error |
at cmsprofiler.ResourceController$$EOlPaMxV.$tt__crawl(ResourceController.groovy:39)
Error |
at cmsprofiler.ResourceController$$DOlPaMxV.$tt__crawl(Unknown Source)
Error |
at cmsprofiler.ResourceController.$tt__crawl(ResourceController.groovy)
Error |
at cmsprofiler.ResourceController$_crawl_closure1.doCall(ResourceController.groovy)
Error |
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Error |
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
Error |
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
Error |
at java.lang.reflect.Method.invoke(Method.java:606)
Error |
at org.springsource.loaded.ri.OriginalClassInvoker.invoke(OriginalClassInvoker.java:47)
Error |
at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1283)
Error |
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
Error |
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
Error |
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1086)
Error |
at groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:1110)
Error |
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:910)
Error |
at groovy.lang.Closure.call(Closure.java:411)
Error |
at cmsprofiler.ResourceController$_crawl_closure1.call(ResourceController.groovy)
Error |
at groovy.lang.Closure.call(Closure.java:427)
Error |
at cmsprofiler.ResourceController$_crawl_closure1.call(ResourceController.groovy)
Error |
at org.codehaus.groovy.grails.orm.support.GrailsTransactionTemplate$1.doInTransaction(GrailsTransactionTemplate.groovy:62)
Error |
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:131)
Error |
at org.codehaus.groovy.grails.orm.support.GrailsTransactionTemplate.execute(GrailsTransactionTemplate.groovy:59)
Error |
at cmsprofiler.ResourceController$$EOlPaMxV.crawl(ResourceController.groovy)
Error |
at cmsprofiler.ResourceController$$DOlPaMxV.crawl(Unknown Source)
Error |
at cmsprofiler.ResourceController.crawl(ResourceController.groovy)
Error |
at cmsprofiler.ResourceController$$EOlPaMxV.crawl(ResourceController.groovy)
Error |
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Error |
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
Error |
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
Error |
at java.lang.reflect.Method.invoke(Method.java:606)
Error |
at org.springsource.loaded.ri.ReloadedTypeInvoker$2.invoke(ReloadedTypeInvoker.java:122)
Error |
at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1283)
Error |
at org.codehaus.groovy.grails.web.servlet.mvc.MixedGrailsControllerHelper.invoke(MixedGrailsControllerHelper.java:154)
Error |
at org.codehaus.groovy.grails.web.servlet.mvc.AbstractGrailsControllerHelper.handleAction(AbstractGrailsControllerHelper.java:354)
Error |
at org.codehaus.groovy.grails.web.servlet.mvc.AbstractGrailsControllerHelper.executeAction(AbstractGrailsControllerHelper.java:231)
Error |
at org.codehaus.groovy.grails.web.servlet.mvc.AbstractGrailsControllerHelper.handleURI(AbstractGrailsControllerHelper.java:197)
Error |
at org.codehaus.groovy.grails.web.servlet.mvc.AbstractGrailsControllerHelper.handleURI(AbstractGrailsControllerHelper.java:121)
Error |
at org.codehaus.groovy.grails.web.servlet.mvc.SimpleGrailsController.handleRequest(SimpleGrailsController.java:72)
Error |
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
Error |
at org.codehaus.groovy.grails.web.servlet.GrailsDispatcherServlet.doDispatch(GrailsDispatcherServlet.java:355)
Error |
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
Error |
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953)
Error |
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:855)
Error |
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
Error |
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)
Error |
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
Error |
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
Error |
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
Error |
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Error |
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
Error |
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
Error |
at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:198)
Error |
at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
Error |
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:343)
Error |
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:260)
Error |
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
Error |
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
Error |
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101)
Error |
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
Error |
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
Error |
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101)
Error |
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
Error |
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
Error |
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101)
Error |
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
Error |
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
Error |
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748)
Error |
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:486)
Error |
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:411)
Error |
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:338)
Error |
at org.codehaus.groovy.grails.web.util.WebUtils.forwardRequestForUrlMappingInfo(WebUtils.java:332)
Error |
at org.codehaus.groovy.grails.web.util.WebUtils.forwardRequestForUrlMappingInfo(WebUtils.java:297)
Error |
at org.codehaus.groovy.grails.web.util.WebUtils.forwardRequestForUrlMappingInfo(WebUtils.java:288)
Error |
at org.codehaus.groovy.grails.web.mapping.filter.UrlMappingsFilter.doFilterInternal(UrlMappingsFilter.java:217)
Error |
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
Error |
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
Error |
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
Error |
at org.codehaus.groovy.grails.web.sitemesh.GrailsPageFilter.executeFilterChainWithWrappedResponse(GrailsPageFilter.java:233)
Error |
at org.codehaus.groovy.grails.web.sitemesh.GrailsPageFilter.obtainContent(GrailsPageFilter.java:208)
Error |
at org.codehaus.groovy.grails.web.sitemesh.GrailsPageFilter.doFilter(GrailsPageFilter.java:153)
Error |
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
Error |
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
Error |
at javax.servlet.FilterChain$doFilter.call(Unknown Source)
Error |
at org.grails.plugin.resource.DevModeSanityFilter.doFilter(DevModeSanityFilter.groovy:45)
Error |
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
Error |
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
Error |
at org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequestFilter.doFilterInternal(GrailsWebRequestFilter.java:69)
Error |
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
Error |
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
Error |
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
Error |
at org.codehaus.groovy.grails.web.filters.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:67)
Error |
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
Error |
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
Error |
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
Error |
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
Error |
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
Error |
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:343)
Error |
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:260)
Error |
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
Error |
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
Error |
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
Error |
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
Error |
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
Error |
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
Error |
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
Error |
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
Error |
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
Error |
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
Error |
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
Error |
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
Error |
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
Error |
at java.lang.Thread.run(Thread.java:745)
Got an Exception: null

请纠正我我在做什么错。尝试了太多替代方法,没有任何效果。谢谢。

最佳答案

用于更新文档的代码可以很简单:

WebSite.collection.update( [ website:website, user:loggedUser.id ], 
[ $set:[ crawled:true ] ] )

注意, websiteloggedUser.id应该是原始类型

关于mongodb - 从Grails更新MongoDB文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25003025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com