Question

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 the 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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top