find()
query on a collection returns a DBCursor
thus DBCursor cursor = collection.find( query ); if( cursor.hasNext() ) DBObject obj = cursor.next();
Warning: Calling toArray
or length
on a DBCursor will irrevocably turn it into an array. This means that, if the cursor was iterating over ten million results (which it was lazily fetching from the database), suddenly there will be a ten-million element array in memory. Before converting to an array, make sure that there are a reasonable number of results using skip()
and limit()
.
For example, to get an array of the 1000-1100th elements of a cursor, use
@dochub cursorsListobj = collection.find( query ).skip( 1000 ).limit( 100 ).toArray();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|