@NotThreadSafe public class DBCursor extends Object implements Cursor, Iterable<DBObject>
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
List<DBObject> obj = collection.find(query).skip(1000).limit(100).toArray();
See Mongo.getDB(String)
for further information about the effective deprecation of this class.Constructor and Description |
---|
DBCursor(DBCollection collection,
DBObject query,
DBObject fields,
ReadPreference readPreference)
Initializes a new database cursor.
|
Modifier and Type | Method and Description |
---|---|
DBCursor |
addOption(int option)
Adds a query option.
|
DBCursor |
addSpecial(String name,
Object value)
Adds a special operator like $maxScan or $returnKey.
|
DBCursor |
batchSize(int numberOfElements)
Limits the number of elements returned in one batch.
|
void |
close()
Terminates this 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.
|
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.
|
int |
getBatchSize()
Gets the batch size.
|
Collation |
getCollation()
Returns the collation options
|
DBCollection |
getCollection()
Gets the collection.
|
long |
getCursorId()
Gets the server's identifier for this Cursor.
|
DBDecoderFactory |
getDecoderFactory()
Gets the decoder factory that creates the decoder this cursor will use to decode objects from MongoDB.
|
DBObject |
getKeysWanted()
Gets the fields to be returned.
|
int |
getLimit()
Gets the query limit.
|
int |
getOptions()
Gets the query options.
|
DBObject |
getQuery()
Gets the query.
|
ReadPreference |
getReadPreference()
Gets the default read preference.
|
ServerAddress |
getServerAddress()
Gets the address of the server that data is pulled from.
|
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 limit)
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 |
numSeen()
Returns the number of objects through which the cursor has iterated.
|
DBObject |
one()
Returns the first document that matches the query.
|
void |
remove() |
DBCursor |
resetOptions()
Resets the query options.
|
DBCursor |
returnKey()
Forces the cursor to only return fields included in the index.
|
DBCursor |
setCollation(Collation collation)
Sets the collation options
|
DBCursor |
setDecoderFactory(DBDecoderFactory factory)
Sets the factory that will be used create a
DBDecoder that will be used to decode BSON documents into DBObject instances. |
DBCursor |
setOptions(int options)
Sets the query option - see Bytes.QUERYOPTION_* for list.
|
DBCursor |
setReadPreference(ReadPreference readPreference)
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 numberOfElements)
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() |
DBObject |
tryNext()
Non blocking check for tailable cursors to see if another object is available.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
forEachRemaining
forEach, spliterator
public DBCursor(DBCollection collection, DBObject query, DBObject fields, ReadPreference readPreference)
collection
- collection to usequery
- the query filter to applyfields
- keys to return from the queryreadPreference
- the read preference for this querypublic DBCursor copy()
public boolean hasNext()
Note: Automatically adds the Bytes.QUERYOPTION_AWAITDATA
option to any cursors with the
Bytes.QUERYOPTION_TAILABLE
option set. For non blocking tailable cursors see tryNext()
.
hasNext
in interface Iterator<DBObject>
public DBObject next()
Note: Automatically adds the Bytes.QUERYOPTION_AWAITDATA
option to any cursors with the
Bytes.QUERYOPTION_TAILABLE
option set. For non blocking tailable cursors see tryNext()
.
next
in interface Iterator<DBObject>
public DBObject tryNext()
Returns the object the cursor is at and moves the cursor ahead by one or return null if no documents is available.
MongoException
- if failedIllegalArgumentException
- if the cursor is not tailablepublic DBObject curr()
public DBCursor addOption(int option)
option
- the option to be addedthis
so calls can be chainedBytes
public DBCursor setOptions(int options)
options
- the bitmask of optionsthis
so calls can be chainedBytes
public DBCursor resetOptions()
this
so calls can be chainedpublic int getOptions()
public int getLimit()
public int getBatchSize()
public DBCursor addSpecial(String name, Object value)
addSpecial("$returnKey", 1) addSpecial("$maxScan", 100)
name
- the name of the special query operatorvalue
- the value of the special query operatorthis
so calls can be chainedpublic DBCursor comment(String comment)
comment
- the comment that is to appear in the profiler outputthis
so calls can be chainedpublic DBCursor maxScan(int max)
max
- the maximum number of documents to returnthis
so calls can be chainedlimit(int)
public DBCursor max(DBObject max)
max
- a document specifying the fields, and the upper bound values for those fieldsthis
so calls can be chainedpublic DBCursor min(DBObject min)
min
- a document specifying the fields, and the lower bound values for those fieldsthis
so calls can be chainedpublic DBCursor returnKey()
this
so calls can be chainedpublic DBCursor showDiskLoc()
$diskLoc
this
so calls can be chainedpublic 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()
this
so calls can be chainedsort(DBObject)
,
hint(DBObject)
public DBObject explain()
DBObject
with a number of fields, including but not limited to:
DBObject
containing the explain output for this DBCursor's queryMongoException
- if the operation failedpublic DBCursor sort(DBObject orderBy)
orderBy
- the fields by which to sortpublic DBCursor limit(int limit)
limit
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.limit
- the number of elements to returnpublic DBCursor batchSize(int numberOfElements)
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.
numberOfElements
- the number of elements to return in a batchthis
so calls can be chainedpublic DBCursor skip(int numberOfElements)
numberOfElements
- the number of elements to skipIllegalStateException
- if the cursor has started to be iterated throughpublic long getCursorId()
Cursor
getCursorId
in interface Cursor
public int numSeen()
public void close()
Cursor
@Deprecated public DBCursor slaveOk()
ReadPreference.secondaryPreferred()
ReadPreference.secondaryPreferred()
public Iterator<DBObject> iterator()
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.
Note that use of this method does not let you call close the underlying cursor in the case of either an exception or an early break. The preferred method of iteration is to use DBCursor as an Iterator, so that you can call close() on it in a finally block.
public List<DBObject> toArray()
MongoException
- if failedpublic List<DBObject> toArray(int max)
max
- the maximum number of objects to returnMongoException
- if failedpublic int count()
MongoException
- if the operation failedsize()
public DBObject one()
public int length()
MongoException
- if failedcount()
,
size()
public int itcount()
MongoException
- if failedcount()
public int size()
MongoException
- if the operation failedcount()
public DBObject getKeysWanted()
public DBObject getQuery()
public DBCollection getCollection()
public ServerAddress getServerAddress()
Cursor
getServerAddress
in interface Cursor
public DBCursor setReadPreference(ReadPreference readPreference)
ReadPreference
for more information.readPreference
- read preference to usethis
so calls can be chainedpublic ReadPreference getReadPreference()
public Collation getCollation()
public DBCursor setCollation(Collation collation)
A null value represents the server default.
collation
- the collation options to usepublic DBCursor setDecoderFactory(DBDecoderFactory factory)
DBDecoder
that will be used to decode BSON documents into DBObject instances.factory
- the DBDecoderFactorythis
so calls can be chainedpublic DBDecoderFactory getDecoderFactory()