由DBCursor的“can't switch cursor access methods”异常引发的思考

先谈谈我是怎么用的:

 DBCollection dbcollection =  XXXXXXXXXX();  //连接mongo
        DBCursor dbCursor = mergeVideoDB.find(XXXX);  //根据name查出若干个
        if (dbCursor.length() == 1) {
            return videoinfos;
        }
        while(dbcursor。hasNext()){    // 这一步产生错误,报出DBCursor的“can‘t switch cursor access methods”
           XXXXXX;     }以上!

首先在使用hasNext方法后需要先通过 _checkType() 检查 cursor 类型。

 1 void _checkType( CursorType type ){
 2         if ( _cursorType == null ){
 3             _cursorType = type;
 4             return;
 5         }
 6
 7         if ( type == _cursorType )
 8             return;
 9
10         throw new IllegalArgumentException( "can‘t switch cursor access methods" );
11     }

回过头来看,  DBCursor dbCursor = mergeVideoDB.find(XXXX); //根据name查出若干个

此时,DBCursor的type为 null

 if (dbCursor.length() == 1) {
            return videoinfos;
        }

在执行完上述步骤后,DBCursor的type变为了array

所以在使用hasNext方法时会报出

"can‘t switch cursor access methods"解决思路:1.不要在之前使用会更改其type的方法进行操作;2.使用除hasNext之外的其他遍历方法进行遍历
直接使用foreach方法进行遍历即可:
for (DBObject dbObject:dbCursor){}。
 

由DBCursor的“can't switch cursor access methods”异常引发的思考

原文地址:https://www.cnblogs.com/hanhaotian/p/9729606.html

时间: 2024-08-28 11:21:26

由DBCursor的“can't switch cursor access methods”异常引发的思考的相关文章

Part3 – OPENVSWICH – Campus Model with Layer2 Access built with Open-Source Applications

In part one we showed how to create Openvswitch extension and submit it to Microcore repository. There were also presented after-install steps for Openvswitch adapted for specific Core needs. http://brezular.com/2011/09/03/part1-openvswich-creating-a

Heterogeneous Self-Organizing Network for Access and Backhaul

This application discloses methods for creating self-organizing networks implemented on heterogeneous mesh networks. The self-organizing networks can include a computing cloud component coupled to the heterogeneous mesh network. In the methods and co

MongoDB - The mongo Shell, Access the mongo Shell Help

In addition to the documentation in the MongoDB Manual, the mongo shell provides some additional information in its “online” help system. This document provides an overview of accessing this help information. Command Line Help To see the list of opti

switch case in protected scope 异常解决

阳光明媚.为何一大早就给我switch报错.异常提示: switch case in protected scope ,当你在switch的case项中只有一行代码的时候,这样做是完全OK的.如果当你想在Case中写下多行代码的时候.这个时候编译器就不淡定了.它会报switch case in protected scope错误. 至此分享一下关于switch case in protected scope 异常解决办法 . Switch会报switch case in protected sc

Java性能提示(全)

http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.htmlComparing the performance of LinkedLists and ArrayLists (and Vectors) (Page last updated May 2001, Added 2001-06-18, Author Jack Shirazi, Publisher OnJava). Tips: ArrayList is faster than

使用ContentProvider

转载请注明出处: http://blog.csdn.net/a992036795/article/details/51610936 一.简介: ContentProvider 在android中的作用是对外共享数据,也就是说可以通过ContentProvider把应用中的数据共享给其他应用访问,其他应用可以通过ContentProvider对应用中的数据进行增.删.该.查.使用ContentProvider的好处是,统一了数据的访问方式. ContentProvider的体层实现实际是Binde

[Android基础系列]重新审视ContentProvider

前言 上一篇文章梳理了一下Service,这一篇文章梳理一下同为四大组件之一的ContentProvider,也试着使用在线MarkDown编辑器.正常开发中,我们使用ContentProvider较少,大家也不会太过于关注.国内能看到的几篇文章也是比较老旧了,demo中也有一些错误,所以写一篇"像样"的,凑凑数. 正文 概述ContentProvider 依旧去API文档中先拿到官方的概述,并做一点蹩脚的翻译和注解: Content providers are one of the

第二十篇:在SOUI中使用分层窗口

从Windows 2K开始,MS为UI开发引入了分层窗口这一窗口风格.使用分层窗口,应用程序的主窗口可以是半透明,也可以是逐点半透明(即每一个像素点的透明度可以不同). 可以说,正是因为有了分层窗口,在Windows上开发的应用程序的UI才真正炫起来. 在UI的主窗口上加一个分层窗口的风格对于一个稍有点UI开发经验的程序员来说是非常简单的,本篇要说的是在SOUI的窗口系统中实现SOUI的分层窗口. 正如使用系统的窗口已经可以实现很漂亮的UI,我们还是会需要DirectUI这样的UI开发技术:有了

SQLiteDatabase

一.SQLite: (一).简介: 除了可以使用文件或SharedPreferences存储数据,还可以选择使用SQLite数据库存储数据. 在Android平台上,集成了一个嵌入式关系型数据库--SQLite. SQLite3支持 NULL.INTEGER.REAL(浮点数字).TEXT(字符串文本)和BLOB(二进制对象)等五种数据类型: 虽然它支持的类型只有五种,但实际上sqlite3也接受varchar(n).char(n).decimal(p,s) 等数据类型,只不过在运算或保存时会转