public class DBCursor extends Object implements Iterator<DBObject>, Iterable<DBObject>, Closeable
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,
ReadPreference preference)
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(String name,
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.
|
DBDecoderFactory |
getDecoderFactory() |
DBObject |
getKeysWanted()
gets the fields to be returned
|
int |
getOptions()
gets the query options
|
DBObject |
getQuery()
gets the query
|
ReadPreference |
getReadPreference()
Gets the default read preference
|
ServerAddress |
getServerAddress()
Gets the Server Address of the server that data is pulled from.
|
List<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(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
|
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.
|
DBCursor |
resetOptions()
resets the query options
|
DBCursor |
setDecoderFactory(DBDecoderFactory fact) |
DBCursor |
setOptions(int options)
sets the query option - see Bytes.QUERYOPTION_* for list
|
DBCursor |
setReadPreference(ReadPreference preference)
Sets the read preference for this cursor.
|
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()
Deprecated.
Replaced with
ReadPreference.secondaryPreferred() |
DBCursor |
snapshot()
Use snapshot mode for the query.
|
DBCursor |
sort(DBObject orderBy)
Sorts this cursor's elements.
|
List<DBObject> |
toArray()
Converts this cursor to an array.
|
List<DBObject> |
toArray(int max)
Converts this cursor to an array.
|
String |
toString() |
public DBCursor(DBCollection collection, DBObject q, DBObject k, ReadPreference preference)
collection
- collection to useq
- query to performk
- keys to return from the querypreference
- the Read Preference for this querypublic DBCursor copy()
public Iterator<DBObject> iterator()
public DBCursor sort(DBObject orderBy)
orderBy
- the fields by which to sortpublic DBCursor addSpecial(String name, Object o)
name
- o
- public DBCursor hint(DBObject indexKeys)
indexKeys
- a DBObject
with fields and directionpublic DBCursor hint(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
MongoException
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 skipIllegalStateException
- if the cursor has started to be iterated throughpublic long getCursorId()
public void close()
close
in interface Closeable
close
in interface AutoCloseable
@Deprecated public DBCursor slaveOk()
ReadPreference.secondaryPreferred()
ReadPreference.secondaryPreferred()
public DBCursor addOption(int option)
option
- public DBCursor setOptions(int options)
options
- public DBCursor resetOptions()
public int getOptions()
public int numGetMores()
public List<Integer> getSizes()
public int numSeen()
public boolean hasNext()
hasNext
in interface Iterator<DBObject>
MongoException
public DBObject next()
next
in interface Iterator<DBObject>
MongoException
public DBObject curr()
public int length()
MongoException
count()
,
size()
public List<DBObject> toArray()
MongoException
public List<DBObject> toArray(int max)
max
- the maximum number of objects to returnMongoException
public int itcount()
MongoException
count()
public int count()
MongoException
size()
public int size()
MongoException
count()
public DBObject getKeysWanted()
public DBObject getQuery()
public DBCollection getCollection()
public ServerAddress getServerAddress()
public DBCursor setReadPreference(ReadPreference preference)
ReadPreference
for more information.preference
- Read Preference to usepublic ReadPreference getReadPreference()
public DBCursor setDecoderFactory(DBDecoderFactory fact)
public DBDecoderFactory getDecoderFactory()