java dbcursor_DBCursor

DBCurosr 是 DBCollection 的 find 方法返回的对象,可以设置 skip、limit 等属性之后,执行查询,得到查询结果,

用法如下:

Java代码

16aa4256b172

Listobj

=collection.find(query).skip(1000).limit(100).toArray();

DBCursor 类的方法可以可以分为两类:修改查询属性 和 执行查询。

以上面的代码为例,find、skip、limit 就是修改查询属性,而 toArrray 就是执行查询。

在实际的实现中,修改查询属性的方法实际上是修改 DBCursor 对象的属性,执行查询则是通过调用 DBCollection._find 得到查询结果。

修改查询属性

Java代码

16aa4256b172

//排序

publicDBCursorsort(DBObjectorderBy)

//添加特殊设置

publicDBCursoraddSpecial(Stringname,Objecto)

//hint,建议MongoDB使用的索引

publicDBCursorhint(DBObjectindexKeys)

//快照

publicDBCursorsnapshot()

//设置返回结果数量

publicDBCursorlimit(intn)

//每次读取的数量

publicDBCursorbatchSize(intn)

//设置开始读取的位置

publicDBCursorskip(intn)

//添加查询设置

publicDBCursoraddOption(intoption)

//指定查询设置

publicvoidsetOptions(intoptions)

以 sort 为例:

Java代码

16aa4256b172

//设置排序

publicDBCursorsort(DBObjectorderBy){

//检查是否已经执行过查询

if(_it!=null)

thrownewIllegalStateException("can'tsortafterexecutingquery");

//设置_ordderBy,返回

_orderBy=orderBy;

returnthis;

}

首先,修改查询属性必须放在执行查询之前,否则将抛异常。

其次,修改查询属性往往会修改 DBCursor 对象的属性,比如上面的代码中修改了 _orderBy 。

修改查询属性是为执行查询做准备,执行查询时会将这些设置合并到查询对象 (_query)中。

执行查询

Java代码

16aa4256b172

//Iterator方式查询,检查是否存在下一个对象

publicbooleanhasNext()

//Iterator方式查询,读取下一个对象

publicDBObjectnext()

//Iterator方式查询,读取当前对象

publicDBObjectcurr()

//Array方式查询,直接获得所有结果

publicListtoArray(intmax)

以 toArray 为例:

Java代码

16aa4256b172

//执行查询,将结果转为list

publicListtoArray(intmax)

throwsMongoException{

//检查cursor类型

_checkType(CursorType.ARRAY);

//执行查询,填充元素

_fill(max);

return_all;

}

执行查询前需要先通过 _checkType() 检查 cursor 类型。

执行 hasNext()、 hasNext()、curr() 后 cursor 类型为 CursorType.ITERATOR;

cusor 类型不为 CursorType.ARRAY 时,是不能调用 toArray 的。

也就是说,得到 curosr 后,要么用 next() 等方式遍历结果,要么将结果直接转为 list,这两种方式是不能混用的

Java代码

16aa4256b172

//检查cursor类型

void_checkType(CursorTypetype){

if(_cursorType==null){

_cursorType=type;

return;

}

if(type==_cursorType)

return;

thrownewIllegalArgumentException("can'tswitchcursoraccessmethods");

}

//执行查询,填充元素

void_fill(intn)

throwsMongoException{

//检查cursor类型

_checkType(CursorType.ARRAY);

//循环调用_next()

while(n>=_all.size()&&_hasNext())

_next();

}

可以看出,Array 方式的查询,实际上是循环调用 _next(),读取所有结果。

_next() 中又通过 _check() 来执行查询,实现如下:

Java代码

16aa4256b172

//下一个元素

privateDBObject_next()

throwsMongoException{

//cursor类型为null时,设置为CursorType.ITERATOR

//执行toArray()时,这里的判断不会通过

if(_cursorType==null)

_checkType(CursorType.ITERATOR);

//执行查询

_check();

_cur=null;

_cur=_it.next();

_num++;

//检查是否只读取部分字段

if(_keysWanted!=null&&_keysWanted.keySet().size()>0){

_cur.markAsPartialObject();

}

//如果cursor类型为CursorType.ARRAY

//将对象添加到结果集中

if(_cursorType==CursorType.ARRAY){

_all.add(_cur);

}

//返回当前对象

return_cur;

}

执行查询实际上是将之前的设置合并,构造出查询对象,然后调用 DBCollection.__find() 得到结果:

Java代码

16aa4256b172

//执行查询

privatevoid_check()

throwsMongoException{

if(_it!=null)

return;

if(_collection!=null&&_query!=null){

//根据查询对象的keySet,自动设置hint

//以便优化查询性能

_lookForHints();

DBObjectfoo=_query;

//特殊查询

if(hasSpecialQueryFields()){

foo=_specialFields==null?newBasicDBObject():_specialFields;

//_addToQueryObject方法调用了query.put(key,dbObject);

//第三个参数表示"dbObject"为空对象(没有keySet)时是否执行put方法,缺省为true

//查询条件

_addToQueryObject(foo,"query",_query,true);

//排序

_addToQueryObject(foo,"orderby",_orderBy,false);

//设置hint

_addToQueryObject(foo,"$hint",_hint);

//执行计划

if(_explain)

foo.put("$explain",true);

//快照

if(_snapshot)

foo.put("$snapshot",true);

}

//通过DBCollection.__find执行查询

_it=_collection.__find(foo,_keysWanted,_skip,_batchSize,_limit,_options);

}

//结果为空

if(_it==null){

_it=(newLinkedList()).iterator();

_fake=true;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值