|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.mongodb.DBCursor
public class 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();
Constructor Summary | |
---|---|
DBCursor(DBCollection collection,
DBObject q,
DBObject k,
ReadPreference preference)
Initializes a new database cursor |
Method Summary | |
---|---|
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.SECONDARY |
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()
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
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 queryMethod Detail |
---|
public DBCursor copy()
public Iterator<DBObject> iterator()
iterator
in interface Iterable<DBObject>
public DBCursor sort(DBObject orderBy)
orderBy
- the fields by which to sort
public DBCursor addSpecial(String name, Object o)
name
- o
-
public DBCursor hint(DBObject indexKeys)
indexKeys
- a DBObject
with fields and direction
public DBCursor hint(String indexName)
indexName
- the name of an index
public 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 query
DBObject
public DBCursor limit(int n)
batchSize(int)
which is the preferred method.
n
- the number of elements to return
public DBCursor batchSize(int n)
n
- the number of elements to return in a batch
public DBCursor skip(int n)
n
- the number of elements to skip
RuntimeException
- if the cursor has started to be iterated throughpublic long getCursorId()
public void close()
@Deprecated public DBCursor slaveOk()
com.mongodb.ReadPreference.SECONDARY
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() throws MongoException
hasNext
in interface Iterator<DBObject>
MongoException
public DBObject next() throws MongoException
next
in interface Iterator<DBObject>
MongoException
public DBObject curr()
public void remove()
remove
in interface Iterator<DBObject>
public int length() throws MongoException
MongoException
count()
,
size()
public List<DBObject> toArray() throws MongoException
MongoException
public List<DBObject> toArray(int max) throws MongoException
max
- the maximum number of objects to return
MongoException
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 DBCursor setReadPreference(ReadPreference preference)
ReadPreference
for more information.
preference
- Read Preference to usepublic ReadPreference getReadPreference()
public DBCursor setDecoderFactory(DBDecoderFactory fact)
public DBDecoderFactory getDecoderFactory()
public String toString()
toString
in class Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |