可以两次迭代mongo游标吗?

时间:2012-03-26 13:19:30

标签: mongodb

换句话说,有没有办法将它倒回到开头?

修改

我正在使用mongo shell和pymongo。

3 个答案:

答案 0 :(得分:17)

Ruby APIrewind!方法完全符合您的要求。

Python API也有cursor.rewind()方法。

PHP API也有cursor.rewind()方法。

但是,Java或C ++ API都没有rewind方法。所有内容都可以从official API page找到。

答案 1 :(得分:1)

您可以使用cursor.reset();

对于PHP:$cursor->reset();

然后在重置后随时运行foreach($cursorData as $data)

答案 2 :(得分:0)

pymongo中的游标有.rewind()方法,你可以参考sample code from previous question with answer that apply

然而,本机mongo shell api不提供此类方法,请参阅method help() on DBQuery object prototype.

> db.collection.find().help()
find() modifiers
        .sort( {...} )
        .limit( n )
        .skip( n )
        .count() - total # of objects matching query, ignores skip,limit
        .size() - total # of objects cursor would return, honors skip,limit
        .explain([verbose])
        .hint(...)
        .showDiskLoc() - adds a $diskLoc field to each returned object

Cursor methods
        .forEach( func )
        .map( func )
        .hasNext()
        .next()
相关问题