java.lang.Object | |
↳ | com.mongodb.DBCursor |
An iterator over database results.
Doing a 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
Listobj = collection.find( query ).skip( 1000 ).limit( 100 ).toArray();
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Initializes a new database cursor
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
adds a query option - see Bytes.QUERYOPTION_* for list
| |||||||||||
adds a special operator like $maxScan or $returnKey
e.g.
| |||||||||||
Limits the number of elements returned in one batch.
| |||||||||||
kills the current cursor on the server.
| |||||||||||
Creates a copy of an existing database cursor.
| |||||||||||
Counts the number of objects matching the query
This does not take limit/skip into consideration
| |||||||||||
Returns the element the cursor is at.
| |||||||||||
Returns an object containing basic information about the
execution of the query that created this cursor
This creates a
DBObject with the key/value pairs:
"cursor" : cursor type
"nScanned" : number of records examined by the database for this query
"n" : the number of records that the database returned
"millis" : how long it took the database to execute the query | |||||||||||
gets the collection
| |||||||||||
gets the cursor id.
| |||||||||||
gets the fields to be returned
| |||||||||||
gets the query options
| |||||||||||
gets the query
| |||||||||||
Gets the default read preference
| |||||||||||
Gets the Server Address of the server that data is pulled from.
| |||||||||||
gets a list containing the number of items received in each batch
| |||||||||||
Checks if there is another object available@return
| |||||||||||
Informs the database of an indexed field of the collection in order to improve performance.
| |||||||||||
Informs the database of indexed fields of the collection in order to improve performance.
| |||||||||||
for testing only!
Iterates cursor and counts objects
| |||||||||||
creates a copy of this cursor object that can be iterated.
| |||||||||||
pulls back all items into an array and returns the number of objects.
| |||||||||||
Limits the number of elements returned.
| |||||||||||
Returns the object the cursor is at and moves the cursor ahead by one.
| |||||||||||
gets the number of times, so far, that the cursor retrieved a batch from the database
| |||||||||||
Returns the number of objects through which the cursor has iterated.
| |||||||||||
Not implemented.
| |||||||||||
resets the query options
| |||||||||||
sets the query option - see Bytes.QUERYOPTION_* for list
| |||||||||||
Sets the read preference for this cursor.
| |||||||||||
Counts the number of objects matching the query
this does take limit/skip into consideration
| |||||||||||
Discards a given number of elements at the beginning of the cursor.
| |||||||||||
This method is deprecated.
Replaced with ReadPreference.SECONDARY
| |||||||||||
Use snapshot mode for the query.
| |||||||||||
Sorts this cursor's elements.
| |||||||||||
Converts this cursor to an array.
| |||||||||||
Converts this cursor to an array.
| |||||||||||
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
| |||||||||||
From interface
java.io.Closeable
| |||||||||||
From interface
java.lang.Iterable
| |||||||||||
From interface
java.util.Iterator
|
Initializes a new database cursor
collection | collection to use |
---|---|
q | query to perform |
k | keys to return from the query |
preference | the Read Preference for this query |
adds a special operator like $maxScan or $returnKey e.g. addSpecial( "$returnKey" , 1 ) e.g. addSpecial( "$maxScan" , 100 )@return
Limits the number of elements returned in one batch. A cursor typically fetches a batch of result objects and store them locally. If batchSize is positive, it represents the size of each batch of objects retrieved. It can be adjusted to optimize performance and limit data transfer. If batchSize is negative, it will limit of number objects returned, that fit within the max batch size limit (usually 4MB), and cursor will be closed. For example if batchSize is -10, then the server will return a maximum of 10 documents and as many as can fit in 4MB, then close the cursor. Note that this feature is different from limit() in that documents must fit within a maximum size, and it removes the need to send a request to close the cursor server-side. The batch size can be changed even after a cursor is iterated, in which case the setting will apply on the next batch retrieval.
n | the number of elements to return in a batch |
---|
kills the current cursor on the server.
Creates a copy of an existing database cursor. The new cursor is an iterator, even if the original was an array.
Counts the number of objects matching the query This does not take limit/skip into consideration
MongoException |
---|
Returns an object containing basic information about the
execution of the query that created this cursor
This creates a DBObject
with the key/value pairs:
"cursor" : cursor type
"nScanned" : number of records examined by the database for this query
"n" : the number of records that the database returned
"millis" : how long it took the database to execute the query
DBObject
gets the cursor id.
gets the query options
Gets the Server Address of the server that data is pulled from. Note that this information may not be available until hasNext() or next() is called.
Informs the database of an indexed field of the collection in order to improve performance.
indexName | the name of an index |
---|
Informs the database of indexed fields of the collection in order to improve performance.
indexKeys | a DBObject with fields and direction |
---|
creates a copy of this cursor object that can be iterated. Note: - you can iterate the DBCursor itself without calling this method - no actual data is getting copied.
pulls back all items into an array and returns the number of objects. Note: this can be resource intensive
MongoException |
---|
Limits the number of elements returned.
Note: parameter n should be positive, although a negative value is supported for legacy reason.
Passing a negative value will call batchSize(int)
which is the preferred method.
n | the number of elements to return |
---|
Returns the object the cursor is at and moves the cursor ahead by one.
MongoException |
---|
gets the number of times, so far, that the cursor retrieved a batch from the database
Returns the number of objects through which the cursor has iterated.
Not implemented.
Sets the read preference for this cursor.
See the * documentation for ReadPreference
for more information.
preference | Read Preference to use |
---|
Counts the number of objects matching the query this does take limit/skip into consideration
MongoException |
---|
Discards a given number of elements at the beginning of the cursor.
n | the number of elements to skip |
---|
RuntimeException | if the cursor has started to be iterated through |
---|
This method is deprecated.
Replaced with ReadPreference.SECONDARY
makes this query ok to run on a slave node
Use snapshot mode for the query. Snapshot mode assures no duplicates are returned, or objects missed, which were present at both the start and end of the query's execution (if an object is new during the query, or deleted during the query, it may or may not be returned, even with snapshot mode). Note that short query responses (less than 1MB) are always effectively snapshotted. Currently, snapshot mode may not be used with sorting or explicit hints.
Sorts this cursor's elements. This method must be called before getting any object from the cursor.
orderBy | the fields by which to sort |
---|
Converts this cursor to an array.
max | the maximum number of objects to return |
---|
MongoException |
---|