@NotThreadSafe public class DBCursor extends Object implements Cursor, 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,
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 |
comment(String comment)
Adds a comment to the query to identify queries in the database profiler output.
|
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()
Deprecated.
there is no replacement for this method
|
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.
|
DBCursor |
max(DBObject max)
Specifies an exclusive upper limit for the index to use in a query.
|
DBCursor |
maxScan(int max)
Limits the number of documents a cursor will return for a query.
|
DBCursor |
maxTime(long maxTime,
TimeUnit timeUnit)
Set the maximum execution time for operations on this cursor.
|
DBCursor |
min(DBObject min)
Specifies an inclusive lower limit for the index to use in a query.
|
DBObject |
next()
Returns the object the cursor is at and moves the cursor ahead by one.
|
int |
numGetMores()
Deprecated.
there is no replacement for this method
|
int |
numSeen()
Returns the number of objects through which the cursor has iterated.
|
DBObject |
one() |
void |
remove()
Not implemented.
|
DBCursor |
resetOptions()
resets the query options
|
DBCursor |
returnKey()
Forces the cursor to only return fields included in the index.
|
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.
|
DBCursor |
showDiskLoc()
Modifies the documents returned to include references to the on-disk location of each document.
|
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 comment(String comment)
public DBCursor maxScan(int max)
limit(int)
public DBCursor max(DBObject max)
public DBCursor min(DBObject min)
public DBCursor returnKey()
public DBCursor showDiskLoc()
$diskLoc
public 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 maxTime(long maxTime, TimeUnit timeUnit)
maxTime
- the maximum time that the server will allow the query to run, before killing the operation. A non-zero value
requires a server version >= 2.6timeUnit
- the time unitpublic 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()
getCursorId
in interface Cursor
public void close()
@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()
@Deprecated public int numGetMores()
@Deprecated 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 DBObject one()
public int size()
MongoException
count()
public DBObject getKeysWanted()
public DBObject getQuery()
public DBCollection getCollection()
public ServerAddress getServerAddress()
getServerAddress
in interface Cursor
public DBCursor setReadPreference(ReadPreference preference)
ReadPreference
for more information.preference
- Read Preference to usepublic ReadPreference getReadPreference()
public DBCursor setDecoderFactory(DBDecoderFactory fact)
public DBDecoderFactory getDecoderFactory()