• 由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){}。
     



  • 相关阅读:
    Scrapy-02-item管道、shell、选择器
    django类视图的装饰器验证
    django禁用csrf
    django admin
    关系型数据库与非关系型数据库
    LINQ.CS
    测试
    测试
    一个测试
    WPF中的Style
  • 原文地址:https://www.cnblogs.com/hanhaotian/p/9729606.html
Copyright © 2020-2023  润新知