Domanda

Ho un database con una colonna "ID", tipo intero.Sto usando:

Records= config.gsupei1Collection.Find (). Ordina ([("ID", Pymonongo.Descending)])

per ottenere il cursore, e poi uso

Record= Records [0]

per ottenere il più grande record ID.

Mi sto chiedendo dopo aver ottenuto 'record', posso usare record.next (), record.prev () per ottenere il record che è vicino al record?Non riesco a trovare nessun documento.

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top