Is it possible to iterate a mongo cursor twice?

前端 未结 3 1877
谎友^
谎友^ 2021-01-17 09:48

In other words, is there a way to rewind it to the beginning?

EDIT

I am using mongo shell and pymongo.

3条回答
  •  轮回少年
    2021-01-17 10:20

    Cursor in pymongo has .rewind() method, you can refer to sample code from previous question with answer that apply.

    Native mongo shell api, however, doesn't provide such method, see 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()
    

提交回复
热议问题