开发者

pymongo: possible to iterate result use next() and prev()?

I have a database with a column "id", integer type. I am using:

records = config.gSupei1Collection.find().sort([("id",pymongo.DESCENDING)])

To get the cursor, and then I use

record = records[0]

to get t开发者_StackOverflowhe largest ID record.

I am wondering after I got 'record', can I use record.next(), record.prev() to get the record that is neighbour to the record? I can't find any document.


You can loop the records. Record is an actual document (a dict), not an iterator, but the result is.

You can do something like this:

for i, d in enumerate(records):
    print i, d

Or if you want to use the returned records as an iterator, you can do something like:

n = records.next()

Or for forward compatibility:

n = next(records)

Records is an iterator. Every time you see something that you can loop, it's an iterable, and you can just call next() and rewind() on it. Be careful though, calling rewind() will reissue the same query to the database server.

FYI, the easiest way to see what type something is is to prototype your program in the interpreter, then you can do >> help(records) and see the pydocs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜