public class MongoCollectionImpl extends AbstractMongoCollection
MongoCollection
interface.MongoCollection.ValidateMode
Modifier and Type | Field and Description |
---|---|
static String |
ID_FIELD_NAME
The name of the canonical id field for MongoDB.
|
DELETE_SINGLE_DELETE_DEFAULT, EMPTY_INDEX_OPTIONS, INSERT_CONTINUE_ON_ERROR_DEFAULT, myClient, myDatabase, myName, UNIQUE_INDEX_OPTIONS, UPDATE_MULTIUPDATE_DEFAULT, UPDATE_UPSERT_DEFAULT
ALL
Constructor and Description |
---|
MongoCollectionImpl(Client client,
MongoDatabase database,
String name)
Create a new MongoDatabaseClient.
|
Modifier and Type | Method and Description |
---|---|
void |
aggregateAsync(Callback<List<Document>> results,
Aggregate command)
Invokes a aggregate command on the server.
|
void |
countAsync(Callback<Long> results,
DocumentAssignable query,
ReadPreference readPreference)
Counts the set of documents matching the query document in the
collection.
|
void |
createIndex(String name,
DocumentAssignable options,
Element... keys)
Creates an index with a generated name, across the keys specified
allowing duplicate entries.
|
protected Query |
createQuery(Find query,
int limit,
int batchSize,
boolean tailable,
boolean awaitData,
boolean immortal)
Creates a properly configured
Query message. |
void |
deleteAsync(Callback<Long> results,
DocumentAssignable query,
boolean singleDelete,
Durability durability)
Deletes a set of documents matching a query from the collection.
|
void |
distinctAsync(Callback<ArrayElement> results,
Distinct command)
Invokes a distinct command on the server.
|
boolean |
drop()
Drops the collection from the database.
|
boolean |
dropIndex(String name)
Deletes the indexes with the provided name.
|
void |
explainAsync(Callback<Document> results,
Find query)
Explains the way that the document will be performed.
|
void |
findAndModifyAsync(Callback<Document> results,
FindAndModify command)
Invokes a findAndModify command on the server.
|
void |
findAsync(Callback<MongoIterator<Document>> results,
Find query)
Finds the set of documents matching the query in the collection.
|
void |
findOneAsync(Callback<Document> results,
Find query)
Finds a single matching document in the collection.
|
void |
groupByAsync(Callback<ArrayElement> results,
GroupBy command)
Invokes a group command on the server.
|
void |
insertAsync(Callback<Integer> results,
boolean continueOnError,
Durability durability,
DocumentAssignable... documents)
Inserts a set of documents into the collection.
|
boolean |
isCapped()
Returns true if the collection
statistics indicate that
the collection is a capped collection. |
void |
mapReduceAsync(Callback<List<Document>> results,
MapReduce command)
Invokes a mapReduce command on the server.
|
void |
saveAsync(Callback<Integer> results,
DocumentAssignable document,
Durability durability)
Saves the
document to the collection. |
Document |
stats()
Returns the statistics for the collection.
|
MongoCursorControl |
streamingFind(StreamCallback<Document> results,
Find query)
Finds the set of documents matching the query in the collection and
streams them to the provided callback one at a time.
|
void |
textSearchAsync(Callback<List<TextResult>> results,
Text command)
Invokes a
text command on the server. |
void |
updateAsync(Callback<Long> results,
DocumentAssignable query,
DocumentAssignable update,
boolean multiUpdate,
boolean upsert,
Durability durability)
Applies updates to a set of documents within the collection.
|
Document |
updateOptions(DocumentAssignable options)
Updates the collection's options or flags using the
collMod
command. |
protected ReadPreference |
updateReadPreference(DocumentBuilder builder,
ReadPreference commandReadPreference,
boolean createQueryElement)
Determines the
ReadPreference to be used based on the command's
ReadPreference or the collection's if the command's
ReadPreference is null . |
Document |
validate(MongoCollection.ValidateMode mode)
Validates the collections contents.
|
aggregate, aggregate, aggregateAsync, aggregateAsync, aggregateAsync, asGetLastError, buildIndexName, count, count, count, count, countAsync, countAsync, countAsync, countAsync, countAsync, countAsync, countAsync, createIndex, createIndex, createIndex, createIndex, delete, delete, delete, delete, deleteAsync, deleteAsync, deleteAsync, deleteAsync, deleteAsync, deleteAsync, deleteAsync, distinct, distinct, distinctAsync, distinctAsync, distinctAsync, dropIndex, explain, explain, explain, explainAsync, explainAsync, explainAsync, find, find, find, findAndModify, findAndModify, findAndModifyAsync, findAndModifyAsync, findAndModifyAsync, findAsync, findAsync, findAsync, findAsync, findAsync, findOne, findOne, findOne, findOneAsync, findOneAsync, findOneAsync, findOneAsync, findOneAsync, getDatabaseName, getDurability, getName, getReadPreference, groupBy, groupBy, groupByAsync, groupByAsync, groupByAsync, insert, insert, insert, insert, insertAsync, insertAsync, insertAsync, insertAsync, insertAsync, insertAsync, insertAsync, mapReduce, mapReduce, mapReduceAsync, mapReduceAsync, mapReduceAsync, save, save, saveAsync, saveAsync, saveAsync, setDurability, setReadPreference, streamingFind, streamingFind, streamingFind, streamingFind, textSearch, textSearch, textSearchAsync, textSearchAsync, textSearchAsync, update, update, update, update, updateAsync, updateAsync, updateAsync, updateAsync, updateAsync, updateAsync, updateAsync
public static final String ID_FIELD_NAME
public MongoCollectionImpl(Client client, MongoDatabase database, String name)
client
- The client for interacting with MongoDB.database
- The database the collection is a part of.name
- The name of the collection we interact with.public void aggregateAsync(Callback<List<Document>> results, Aggregate command) throws MongoDbException
This is the canonical aggregate
method that implementations
must override.
Overridden to construct a aggregate command and send it to the server.
aggregateAsync
in interface MongoCollection
aggregateAsync
in class AbstractMongoCollection
results
- Callback for the aggregation results returned.command
- The details of the aggregation request.MongoDbException
- On an error executing the aggregate command.MongoCollection.aggregateAsync(Callback, Aggregate)
public void countAsync(Callback<Long> results, DocumentAssignable query, ReadPreference readPreference) throws MongoDbException
This is the canonical count
method that implementations must
override.
This is the canonical count
method that implementations must
override.
countAsync
in interface MongoCollection
countAsync
in class AbstractMongoCollection
results
- The callback to notify of the results.query
- The query document.readPreference
- The preference for which servers to use to retrieve the
results.MongoDbException
- On an error finding the documents.public void createIndex(String name, DocumentAssignable options, Element... keys) throws MongoDbException
This method is intended to be used with the
Index
class's static methods:
import static
com.allanbank.mongodb.bson.builder.BuilderFactory.start
; import staticcom.allanbank.mongodb.builder.Index.asc
; import staticcom.allanbank.mongodb.builder.Index.desc
; MongoCollection collection = ...; collection.createIndex("sparse_f", start().add("sparse", true), asc("f") ); ...
This is the canonical createIndex
method that
implementations must override.
Overridden to insert the index document into the 'system.indexes' collection.
createIndex
in interface MongoCollection
createIndex
in class AbstractMongoCollection
name
- The name of the index. If null
then a name is
generated based on the keys.options
- The options for the index.keys
- The keys to use for the index.MongoDbException
- On a failure building the index.MongoCollection.createIndex(String,DocumentAssignable,Element...)
public void deleteAsync(Callback<Long> results, DocumentAssignable query, boolean singleDelete, Durability durability) throws MongoDbException
This is the canonical delete
method that implementations
must override.
Overridden to send a Delete
message to the server.
deleteAsync
in interface MongoCollection
deleteAsync
in class AbstractMongoCollection
results
- Callback that will be notified of the results of the query. If
the durability of the operation is NONE then this will be -1.query
- Query to locate the documents to be deleted.singleDelete
- If true then only a single document will be deleted. If
running in a sharded environment then this field must be false
or the query must contain the shard key.durability
- The durability for the delete.MongoDbException
- On an error deleting the documents.MongoCollection.deleteAsync(Callback, DocumentAssignable, boolean,
Durability)
public void distinctAsync(Callback<ArrayElement> results, Distinct command) throws MongoDbException
This is the canonical disitnct
method that implementations
must override.
Overridden to construct a 'distinct' command and send it to the server.
distinctAsync
in interface MongoCollection
distinctAsync
in class AbstractMongoCollection
results
- Callback for the distinct results returned.command
- The details of the distinct request.MongoDbException
- On an error finding the documents.public boolean drop()
Overridden to issue a { "drop" :
MongoCollection.drop()
public boolean dropIndex(String name) throws MongoDbException
This is the canonical dropIndex
method that implementations
must override.
Overridden to issue a { "deleteIndexes" :
dropIndex
in interface MongoCollection
dropIndex
in class AbstractMongoCollection
name
- The name of the index.MongoDbException
- On an error deleting the indexes.public void explainAsync(Callback<Document> results, Find query) throws MongoDbException
This is the canonical count
method that implementations must
override.
Overridden to send a Query
message to the server to explain the
Find
's query.
explainAsync
in interface MongoCollection
explainAsync
in class AbstractMongoCollection
results
- Callback that will be notified of the results of the explain.query
- The query details.MongoDbException
- On an error finding the documents.public void findAndModifyAsync(Callback<Document> results, FindAndModify command) throws MongoDbException
This is the canonical findAndModify
method that
implementations must override.
Overridden to send an Command
findAndModify message to the
server.
findAndModifyAsync
in interface MongoCollection
findAndModifyAsync
in class AbstractMongoCollection
results
- Callback for the the found document.command
- The details of the find and modify request.MongoDbException
- On an error finding the documents.MongoCollection.findAndModifyAsync(Callback, FindAndModify)
public void findAsync(Callback<MongoIterator<Document>> results, Find query) throws MongoDbException
This is the canonical find
method that implementations must
override.
Overridden to send a Query
message to the server.
findAsync
in interface MongoCollection
findAsync
in class AbstractMongoCollection
results
- Callback that will be notified of the results of the query.query
- The query details.MongoDbException
- On an error finding the documents.MongoCollection.findAsync(Callback, Find)
public void findOneAsync(Callback<Document> results, Find query) throws MongoDbException
Note that following options in the Find
class do not make sense
and are silently ignored by this method.
Batch Size
- Automatically set to 1.Limit
- Automatically set to 1.Tailable
- This method only returns 1
document.
This is the canonical findOne
method that implementations
must override.
Overridden to send a Query
message to the server.
findOneAsync
in interface MongoCollection
findOneAsync
in class AbstractMongoCollection
results
- Callback that will be notified of the results of the query.query
- The query details.MongoDbException
- On an error finding the document.MongoCollection.findOneAsync(Callback, DocumentAssignable)
public void groupByAsync(Callback<ArrayElement> results, GroupBy command) throws MongoDbException
This is the canonical groupBy
method that implementations
must override.
Overridden to construct a group command and send it to the server.
groupByAsync
in interface MongoCollection
groupByAsync
in class AbstractMongoCollection
results
- Callback for the group results returned.command
- The details of the group request.MongoDbException
- On an error finding the documents.public void insertAsync(Callback<Integer> results, boolean continueOnError, Durability durability, DocumentAssignable... documents) throws MongoDbException
This is the canonical insert
method that implementations
must override.
Overridden to send an Insert
message to the server.
insertAsync
in interface MongoCollection
insertAsync
in class AbstractMongoCollection
results
- Callback
that will be notified with the results of the
insert. Currently, the value is always zero. Once SERVER-4381 is fixed then expected to be the number of
documents inserted. If the durability is NONE then returns
-1
.continueOnError
- If the insert should continue if one of the documents causes
an error.durability
- The durability for the insert.documents
- The documents to add to the collection.MongoDbException
- On an error inserting the documents.MongoCollection.insertAsync(Callback, boolean, Durability,
DocumentAssignable[])
public boolean isCapped() throws MongoDbException
statistics
indicate that
the collection is a capped collection.
Overridden to send a collStats
command to the MongoDB server and
look for the capped
field to determine if the collection is
capped or not.
statistics
indicate that
the collection is a capped collection.MongoDbException
- On an error collecting the collection statistics.MongoCollection.isCapped()
public void mapReduceAsync(Callback<List<Document>> results, MapReduce command) throws MongoDbException
This is the canonical mapReduce
method that implementations
must override.
Overridden to construct a mapReduce command and send it to the server.
mapReduceAsync
in interface MongoCollection
mapReduceAsync
in class AbstractMongoCollection
results
- Callback for the map/reduce results returned. Note this might
be empty if the output type is not inline.command
- The details of the map/reduce request.MongoDbException
- On an error finding the documents.MongoCollection.mapReduceAsync(Callback, MapReduce)
public void saveAsync(Callback<Integer> results, DocumentAssignable document, Durability durability) throws MongoDbException
document
to the collection.
If the document
does not contain an _id
field then this
method is equivalent to:
insertAsync(results, durability, document)
.
If the document
does contain an _id
field then this
method is equivalent to:
updateAsync(results, BuilderFactory.start().add(document.get("_id")),
document, false, true, durability)
.
This is the canonical save
method that implementations must
override.
Overridden to save the document.
saveAsync
in interface MongoCollection
saveAsync
in class AbstractMongoCollection
results
- Callback
that will be notified with the results of the
insert. If the durability of the operation is NONE then this
will be -1.document
- The document to save to the collection.durability
- The durability for the save.MongoDbException
- On an error saving the documents.public Document stats() throws MongoDbException
Overridden to send a collStats
command to the MongoDB server.
MongoDbException
- On an error collecting the collection statistics.MongoCollection.stats()
public MongoCursorControl streamingFind(StreamCallback<Document> results, Find query) throws MongoDbException
The sequence of callbacks will be terminated by either calling the
results.done()
method or by calling the
results.exception(...)
method
(in the event of an error).
Applications can terminate the stream by throwing a
RuntimeException
from the Callback.callback(V)
method
(which will then call the Callback.exception(java.lang.Throwable)
method or by
closing the MongoCursorControl
returned from this method.
Only a single thread will invoke the callback at a time but that thread may change over time.
If the callback processing requires any significant time (including I/O)
it is recommended that an
Executor
be configured within the MongoClientConfiguration
to
off-load the processing from the receive thread.
This is the canonical streamingFind
method that
implementations must override.
Overridden to send a Query
message to the server and setup the
streaming query callback.
streamingFind
in interface MongoCollection
streamingFind
in class AbstractMongoCollection
results
- Callback that will be notified of the results of the query.query
- The query details.MongoCursorControl
to control the cursor streaming
documents to the caller. This includes the ability to stop the
cursor and persist its state.MongoDbException
- On an error finding the documents.public void textSearchAsync(Callback<List<TextResult>> results, Text command) throws MongoDbException
text
command on the server.
This is the canonical textSearch
method that implementations
must override.
Overridden to construct a text command and send it to the server.
textSearchAsync
in interface MongoCollection
textSearchAsync
in class AbstractMongoCollection
results
- Callback for the text
results returned.command
- The details of the text
request.MongoDbException
- On an error executing the text
command.MongoCollection.textSearchAsync(Callback, Text)
public void updateAsync(Callback<Long> results, DocumentAssignable query, DocumentAssignable update, boolean multiUpdate, boolean upsert, Durability durability) throws MongoDbException
This is the canonical update
method that implementations
must override.
Overridden to send an Update
message to the server.
updateAsync
in interface MongoCollection
updateAsync
in class AbstractMongoCollection
results
- The Callback
that will be notified of the number of
documents updated. If the durability of the operation is NONE
then this will be -1.query
- The query to select the documents to update.update
- The updates to apply to the selected documents.multiUpdate
- If true then the update is applied to all of the matching
documents, otherwise only the first document found is updated.upsert
- If true then if no document is found then a new document is
created and updated, otherwise no operation is performed.durability
- The durability for the update.MongoDbException
- On an error updating the documents.MongoCollection.updateAsync(Callback, DocumentAssignable,
DocumentAssignable, boolean, boolean, Durability)
public Document updateOptions(DocumentAssignable options) throws MongoDbException
collMod
command. The return value is the response from the MongoDB server and
normally contains a <name>_old
field for each
successfully set option on the collection. MongoCollection collection = ...; collection.updateOptions( BuilderFactory.start().add( "usePowerOf2Sizes", true ) );
Overridden to send a collMod
command to the server.
options
- The collection options to be set.MongoDbException
- On an error validating the collection.public Document validate(MongoCollection.ValidateMode mode) throws MongoDbException
Overridden to send a validate
command to the server.
mode
- The validation mode to use.MongoDbException
- On an error validating the collection.protected Query createQuery(Find query, int limit, int batchSize, boolean tailable, boolean awaitData, boolean immortal)
Query
message.query
- The Find
to construct the Query
from.limit
- The limit for the query.batchSize
- The batch size for the query.tailable
- If the query should create a tailable cursor.awaitData
- If the query should await data.immortal
- If the query should create a cursor that does not timeout,
e.g., immortal.Query
message.protected ReadPreference updateReadPreference(DocumentBuilder builder, ReadPreference commandReadPreference, boolean createQueryElement)
ReadPreference
to be used based on the command's
ReadPreference
or the collection's if the command's
ReadPreference
is null
. Updates the command's
DocumentBuilder
with the ReadPreference
details if
connected to a sharded cluster and the resulting ReadPreference
is not supported by the legacy settings.builder
- The builder for the command document to augment with the read
preferences if connected to a sharded cluster.commandReadPreference
- The read preferences from the command.createQueryElement
- If true then the existing builder's contents will be pushed
into a $query sub-document. This is required to ensure the
command is not rejected by the mongod
after processing
by the mongos
.ReadPreference
to use.Copyright © 2011-2013 Allanbank Consulting, Inc.. All Rights Reserved.