NutzCN Logo
问答 nutz-plugins-mongodb 查询指定key返回值错误
发布于 2192天前 作者 Chaokun 1488 次浏览 复制 上一个帖子 下一个帖子
标签:

MongoDB 数据格式

key1:{
key1_1:value
key1_2:value
}

我想只返回

key1:{
key1_1:value
}

ZMoDoc keys = ZMoDoc .NEW();
keys.set("key1.key1_1", 1);
List<Test> list = this.zMoCo.query(Test.class, cnd, keys, null);

查询时报错

com.mongodb.MongoQueryException: Query failed with error code 2 and error message '>1 field in obj: { key1.key1_1: 1}
8 回复

@wendal 请问这种情况应该怎么试用这个插件?

做不到吧,只能限定第一层的key吧

贴上完整报错信息吧

@wendal 嗯,MongoDB是支持的,我写原生的是可以查询

18-12-22 12:52:27.089 DEBUG [http-nio-8888-exec-2] db.dailyData.find({ "ProjectId" : { "$eq" : "ac17ea95ae5d438b9000efce3fec6ee1" }, "Date" : { "$gte" : "2018-08-01", "$lte" : "2018-08-31" } }, { "$set" : { "Date" : 1, "Data.System.RunSCOP" : 1, "Data.System.RunUCOP" : 1, "Data.System.SECU" : 1, "Data.System.ROutB" : 1, "Data.System.RefP" : 1 } })
com.mongodb.MongoQueryException: Query failed with error code 2 and error message '>1 field in obj: { Date: 1, Data.System.RunSCOP: 1, Data.System.RunUCOP: 1, Data.System.SECU: 1, Data.System.ROutB: 1, Data.System.RefP: 1 }' on server 123.57.15.76:27017
	at com.mongodb.operation.FindOperation$1.call(FindOperation.java:706)
	at com.mongodb.operation.FindOperation$1.call(FindOperation.java:695)
	at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:457)
	at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:401)
	at com.mongodb.operation.FindOperation.execute(FindOperation.java:695)
	at com.mongodb.operation.FindOperation.execute(FindOperation.java:83)
	at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:179)
	at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:161)
	at com.mongodb.DBCursor.initializeCursor(DBCursor.java:896)
	at com.mongodb.DBCursor.hasNext(DBCursor.java:148)
	at org.nutz.mongo.ZMoCo.query(ZMoCo.java:689)
	at org.nutz.mongo.ZMoCo.query(ZMoCo.java:637)
	at com.proton.creek.calculate.service.impl.HvacSystemServiceImpl.energyCalendar(HvacSystemServiceImpl.java:258)
	at com.proton.creek.calculate.action.HvacSystemContorller.energyCalendar(HvacSystemContorller.java:102)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)
	at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:112)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61)
	at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)
	at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)
	at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)
	at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)
	at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
	at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
	at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
	at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
	at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)
	at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:748)

待我查一查

我试了一下

        ZMoCo co = mongo.getCollection("obj");
        DBCursor cur = co.find(ZMoDoc.NEW("id", "vvc7h6utpmi2tpf5b0jk7dgnhd"), ZMoDoc.NEW("key1.key1_1", 1));
        DBObject dbo = cur.next();
        System.out.println(dbo);

数据是这样的:

{
   id: "vvc7h6utpmi2tpf5b0jk7dgnhd",
   key1: {
      key1_1: 123,
      key1_2: 456
   }
}

输出是这样:

{ "_id" : { "$oid" : "5c1f9811c735ec4734f6bb69"} , "key1" : { "key1_1" : 123}}

并没有报错

@wendal ,谢谢。按你的写法可以查询后,我找到我的问题了,赋值的时候用的set

ZMoDoc keys = ZMoDoc .NEW();
keys.set("key1.key1_1", 1);

应该用put

添加回复
请先登陆
回到顶部