public class DBCursor extends java.lang.Object implements java.util.Iterator<DBObject>, java.lang.Iterable<DBObject>
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();
Constructor and Description |
---|
DBCursor(DBCollection collection,
DBObject q,
DBObject k)
Initializes a new database cursor
|
Modifier and Type | Method and Description |
---|---|
DBCursor |
addOption(int option)
adds a query option - see Bytes.QUERYOPTION_* for list
|
DBCursor |
addSpecial(java.lang.String name,
java.lang.Object o)
adds a special operator like $maxScan or $returnKey
e.g.
|
DBCursor |
batchSize(int n)
Limits the number of elements returned in one batch.
|
void |
close()
kills the current cursor on the server.
|
DBCursor |
copy()
Creates a copy of an existing database cursor.
|
int |
count()
Counts the number of objects matching the query
This does not take limit/skip into consideration
|
DBObject |
curr()
Returns the element the cursor is at.
|
DBObject |
explain()
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 |
DBCollection |
getCollection()
gets the collection
|
long |
getCursorId()
gets the cursor id.
|
DBObject |
getKeysWanted()
gets the fields to be returned
|
int |
getOptions()
gets the query options
|
DBObject |
getQuery()
gets the query
|
ServerAddress |
getServerAddress()
Gets the Server Address of the server that data is pulled from.
|
java.util.List<java.lang.Integer> |
getSizes()
gets a list containing the number of items received in each batch
|
boolean |
hasNext()
Checks if there is another object available
|
DBCursor |
hint(DBObject indexKeys)
Informs the database of indexed fields of the collection in order to improve performance.
|
DBCursor |
hint(java.lang.String indexName)
Informs the database of an indexed field of the collection in order to improve performance.
|
int |
itcount()
for testing only!
Iterates cursor and counts objects
|
java.util.Iterator<DBObject> |
iterator()
creates a copy of this cursor object that can be iterated.
|
int |
length()
pulls back all items into an array and returns the number of objects.
|
DBCursor |
limit(int n)
Limits the number of elements returned.
|
DBObject |
next()
Returns the object the cursor is at and moves the cursor ahead by one.
|
int |
numGetMores()
gets the number of times, so far, that the cursor retrieved a batch from the database
|
int |
numSeen()
Returns the number of objects through which the cursor has iterated.
|
void |
remove()
Not implemented.
|
void |
resetOptions()
resets the query options
|
void |
setOptions(int options)
sets the query option - see Bytes.QUERYOPTION_* for list
|
int |
size()
Counts the number of objects matching the query
this does take limit/skip into consideration
|
DBCursor |
skip(int n)
Discards a given number of elements at the beginning of the cursor.
|
DBCursor |
slaveOk()
makes this query ok to run on a slave node
|
DBCursor |
snapshot()
Use snapshot mode for the query.
|
DBCursor |
sort(DBObject orderBy)
Sorts this cursor's elements.
|
java.util.List<DBObject> |
toArray()
Converts this cursor to an array.
|
java.util.List<DBObject> |
toArray(int max)
Converts this cursor to an array.
|
java.lang.String |
toString() |
public DBCursor(DBCollection collection, DBObject q, DBObject k)
collection
- collection to useq
- query to performk
- keys to return from the querypublic DBCursor copy()
public java.util.Iterator<DBObject> iterator()
iterator
in interface java.lang.Iterable<DBObject>
public DBCursor sort(DBObject orderBy)
orderBy
- the fields by which to sortpublic DBCursor addSpecial(java.lang.String name, java.lang.Object o)
name
- o
- public DBCursor hint(DBObject indexKeys)
indexKeys
- a DBObject
with fields and directionpublic DBCursor hint(java.lang.String indexName)
indexName
- the name of an indexpublic DBCursor snapshot()
public DBObject explain()
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 queryDBObject
public DBCursor limit(int n)
batchSize(int)
which is the preferred method.n
- the number of elements to returnpublic DBCursor batchSize(int n)
n
- the number of elements to return in a batchpublic DBCursor skip(int n)
n
- the number of elements to skipjava.lang.RuntimeException
- if the cursor has started to be iterated throughpublic long getCursorId()
public void close()
public DBCursor slaveOk()
public DBCursor addOption(int option)
option
- public void setOptions(int options)
options
- public void resetOptions()
public int getOptions()
public int numGetMores()
public java.util.List<java.lang.Integer> getSizes()
public int numSeen()
public boolean hasNext() throws MongoException
hasNext
in interface java.util.Iterator<DBObject>
MongoException
public DBObject next() throws MongoException
next
in interface java.util.Iterator<DBObject>
MongoException
public DBObject curr()
public void remove()
remove
in interface java.util.Iterator<DBObject>
public int length() throws MongoException
MongoException
count()
,
size()
public java.util.List<DBObject> toArray() throws MongoException
MongoException
public java.util.List<DBObject> toArray(int max) throws MongoException
max
- the maximum number of objects to returnMongoException
public int itcount()
count()
public int count() throws MongoException
MongoException
size()
public int size() throws MongoException
MongoException
count()
public DBObject getKeysWanted()
public DBObject getQuery()
public DBCollection getCollection()
public ServerAddress getServerAddress()
public java.lang.String toString()
toString
in class java.lang.Object