Interface MongoClient
public interface MongoClient
A Vert.x service used to interact with MongoDB server instances.
Some of the operations might change _id field of passed JsonObject document.
- Author:
- Tim Fox
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionRun aggregate MongoDB command with defaultAggregateOptions.aggregateWithOptions(String collection, JsonArray pipeline, AggregateOptions options) Run aggregate MongoDB command.bulkWrite(String collection, List<BulkOperation> operations) Execute a bulk operation.bulkWriteWithOptions(String collection, List<BulkOperation> operations, BulkWriteOptions bulkWriteOptions) Execute a bulk operation with the specified write options.close()Close the client and release its resourcescount(String collection, JsonObject query) Count matching documents in a collection.countWithOptions(String collection, JsonObject query, CountOptions countOptions) Count matching documents in a collection.static MongoClientcreate(Vertx vertx, JsonObject config) Create a Mongo client which maintains its own data source.createCollection(String collectionName) Create a new collectioncreateCollectionWithOptions(String collectionName, CreateCollectionOptions collectionOptions) Create a new collection with optionsCreates aMongoGridFsClientused to interact with Mongo GridFS.createGridFsBucketService(String bucketName) Creates aMongoGridFsClientused to interact with Mongo GridFS.createIndex(String collection, JsonObject key) Creates an index.createIndexes(String collection, List<IndexModel> indexes) creates an indexescreateIndexWithOptions(String collection, JsonObject key, IndexOptions options) Creates an index.static MongoClientcreateShared(Vertx vertx, JsonObject config) LikecreateShared(io.vertx.core.Vertx, JsonObject, String)but with the default data source namestatic MongoClientcreateShared(Vertx vertx, JsonObject config, String dataSourceName) Create a Mongo client which shares its data source with any other Mongo clients created with the same data source namestatic MongoClientcreateWithMongoSettings(Vertx vertx, JsonObject config, String dataSourceName, com.mongodb.MongoClientSettings settings) Constructor targeting the jvm, like standard constructorcreateShared(Vertx, JsonObject, String), but it accepts default mongoClientSettings to configure mongoGets the distinct values of the specified field name.distinct(String collection, String fieldName, String resultClassname, DistinctOptions distinctOptions) Gets the distinct values of the specified field name.distinctBatch(String collection, String fieldName, String resultClassname) Gets the distinct values of the specified field name.distinctBatch(String collection, String fieldName, String resultClassname, DistinctOptions distinctOptions) Gets the distinct values of the specified field name.distinctBatchWithQuery(String collection, String fieldName, String resultClassname, JsonObject query) Gets the distinct values of the specified field name filtered by specified query.distinctBatchWithQuery(String collection, String fieldName, String resultClassname, JsonObject query, int batchSize) Gets the distinct values of the specified field name filtered by specified query.distinctBatchWithQuery(String collection, String fieldName, String resultClassname, JsonObject query, int batchSize, DistinctOptions distinctOptions) Gets the distinct values of the specified field name filtered by specified query.distinctBatchWithQuery(String collection, String fieldName, String resultClassname, JsonObject query, DistinctOptions distinctOptions) Gets the distinct values of the specified field name filtered by specified query.distinctWithQuery(String collection, String fieldName, String resultClassname, JsonObject query) Gets the distinct values of the specified field name filtered by specified query.distinctWithQuery(String collection, String fieldName, String resultClassname, JsonObject query, DistinctOptions distinctOptions) Gets the distinct values of the specified field name filtered by specified query.dropCollection(String collection) Drop a collectiondropIndex(String collection, JsonObject key) Drops the index given the keys used to create it.Drops the index given its name.find(String collection, JsonObject query) Find matching documents in the specified collectionfindBatch(String collection, JsonObject query) Find matching documents in the specified collection.findBatchWithOptions(String collection, JsonObject query, FindOptions options) Find matching documents in the specified collection, specifying options.findOne(String collection, JsonObject query, JsonObject fields) Find a single matching document in the specified collectionfindOneAndDelete(String collection, JsonObject query) Find a single matching document in the specified collection and delete it.findOneAndDeleteWithOptions(String collection, JsonObject query, FindOptions findOptions) Find a single matching document in the specified collection and delete it.findOneAndReplace(String collection, JsonObject query, JsonObject replace) Find a single matching document in the specified collection and replace it.findOneAndReplaceWithOptions(String collection, JsonObject query, JsonObject replace, FindOptions findOptions, UpdateOptions updateOptions) Find a single matching document in the specified collection and replace it.findOneAndUpdate(String collection, JsonObject query, JsonObject update) Find a single matching document in the specified collection and update it.findOneAndUpdateWithOptions(String collection, JsonObject query, JsonObject update, FindOptions findOptions, UpdateOptions updateOptions) Find a single matching document in the specified collection and update it.findWithOptions(String collection, JsonObject query, FindOptions options) Find matching documents in the specified collection, specifying optionsGet a list of all collections in the database.insert(String collection, JsonObject document) Insert a document in the specified collectioninsertWithOptions(String collection, JsonObject document, WriteOption writeOption) Insert a document in the specified collection with the specified write optionlistIndexes(String collection) Get all the indexes in this collection.removeDocument(String collection, JsonObject query) Remove a single matching document from a collection and return the handler withMongoClientDeleteResultresultremoveDocuments(String collection, JsonObject query) Remove matching documents from a collection and return the handler withMongoClientDeleteResultresultremoveDocumentsWithOptions(String collection, JsonObject query, WriteOption writeOption) Remove matching documents from a collection with the specified write option and return the handler withMongoClientDeleteResultresultremoveDocumentWithOptions(String collection, JsonObject query, WriteOption writeOption) Remove a single matching document from a collection with the specified write option and return the handler withMongoClientDeleteResultresultrenameCollection(String oldCollectionName, String newCollectionName) Rename a collectionrenameCollectionWithOptions(String oldCollectionName, String newCollectionName, RenameCollectionOptions collectionOptions) Rename a collectionreplaceDocuments(String collection, JsonObject query, JsonObject replace) Replace matching documents in the specified collection and return the handler withMongoClientUpdateResultresultreplaceDocumentsWithOptions(String collection, JsonObject query, JsonObject replace, UpdateOptions options) Replace matching documents in the specified collection, specifying options and return the handler withMongoClientUpdateResultresultrunCommand(String commandName, JsonObject command) Run an arbitrary MongoDB command.save(String collection, JsonObject document) Save a document in the specified collectionsaveWithOptions(String collection, JsonObject document, WriteOption writeOption) Save a document in the specified collection with the specified write optionupdateCollection(String collection, JsonObject query, JsonArray update) Use an aggregation pipeline to update documents in the specified collection and return the handler withMongoClientUpdateResultresultupdateCollection(String collection, JsonObject query, JsonObject update) Update matching documents in the specified collection and return the handler withMongoClientUpdateResultresultupdateCollectionWithOptions(String collection, JsonObject query, JsonArray update, UpdateOptions options) Use an aggregation pipeline to update documents in the specified collection, specifying options and return the handler withMongoClientUpdateResultresultupdateCollectionWithOptions(String collection, JsonObject query, JsonObject update, UpdateOptions options) Update matching documents in the specified collection, specifying options and return the handler withMongoClientUpdateResultresultReadStream<com.mongodb.client.model.changestream.ChangeStreamDocument<JsonObject>> Watch the collection change.
-
Field Details
-
DEFAULT_POOL_NAME
-
DEFAULT_DB_NAME
-
-
Method Details
-
create
Create a Mongo client which maintains its own data source.- Parameters:
vertx- the Vert.x instanceconfig- the configuration- Returns:
- the client
-
createWithMongoSettings
static MongoClient createWithMongoSettings(Vertx vertx, JsonObject config, String dataSourceName, com.mongodb.MongoClientSettings settings) Constructor targeting the jvm, like standard constructorcreateShared(Vertx, JsonObject, String), but it accepts default mongoClientSettings to configure mongo- Parameters:
vertx- the Vert.x instanceconfig- the configuration use only to provide objectId and database namedataSourceName- the data source namesettings- the native java mongo settings- Returns:
- the client
-
save
Save a document in the specified collectionThis operation might change _id field of document parameter
- Parameters:
collection- the collectiondocument- the document- Returns:
- a future provided with the id if document didn't already have one
-
saveWithOptions
Save a document in the specified collection with the specified write optionThis operation might change _id field of document parameter
- Parameters:
collection- the collectiondocument- the documentwriteOption- the write option to use- Returns:
- a future provided with the id if document didn't already have one
-
insert
Insert a document in the specified collectionThis operation might change _id field of document parameter
- Parameters:
collection- the collectiondocument- the document- Returns:
- a future provided with the id if document didn't already have one
-
insertWithOptions
Insert a document in the specified collection with the specified write optionThis operation might change _id field of document parameter
- Parameters:
collection- the collectiondocument- the documentwriteOption- the write option to use- Returns:
- a future provided with the id if document didn't already have one
-
updateCollection
Future<MongoClientUpdateResult> updateCollection(String collection, JsonObject query, JsonObject update) Update matching documents in the specified collection and return the handler withMongoClientUpdateResultresult- Parameters:
collection- the collectionquery- query used to match the documentsupdate- used to describe how the documents will be updated- Returns:
- a future notified with a
MongoClientUpdateResultwhen complete
-
updateCollection
Future<MongoClientUpdateResult> updateCollection(String collection, JsonObject query, JsonArray update) Use an aggregation pipeline to update documents in the specified collection and return the handler withMongoClientUpdateResultresult- Parameters:
collection- the collectionquery- query used to match the documentsupdate- used to describe how the documents will be updated- Returns:
- a future notified with a
MongoClientUpdateResultwhen complete
-
updateCollectionWithOptions
Future<MongoClientUpdateResult> updateCollectionWithOptions(String collection, JsonObject query, JsonObject update, UpdateOptions options) Update matching documents in the specified collection, specifying options and return the handler withMongoClientUpdateResultresult- Parameters:
collection- the collectionquery- query used to match the documentsupdate- used to describe how the documents will be updatedoptions- options to configure the update- Returns:
- a future notified with a
MongoClientUpdateResultwhen complete
-
updateCollectionWithOptions
Future<MongoClientUpdateResult> updateCollectionWithOptions(String collection, JsonObject query, JsonArray update, UpdateOptions options) Use an aggregation pipeline to update documents in the specified collection, specifying options and return the handler withMongoClientUpdateResultresult- Parameters:
collection- the collectionquery- query used to match the documentsupdate- aggregation pipeline used to describe how documents will be updatedoptions- options to configure the update- Returns:
- a future notified with a
MongoClientUpdateResultwhen complete
-
replaceDocuments
Future<MongoClientUpdateResult> replaceDocuments(String collection, JsonObject query, JsonObject replace) Replace matching documents in the specified collection and return the handler withMongoClientUpdateResultresult- Parameters:
collection- the collectionquery- query used to match the documentsreplace- all matching documents will be replaced with this- Returns:
- a future notified with a
MongoClientUpdateResultwhen complete
-
replaceDocumentsWithOptions
Future<MongoClientUpdateResult> replaceDocumentsWithOptions(String collection, JsonObject query, JsonObject replace, UpdateOptions options) Replace matching documents in the specified collection, specifying options and return the handler withMongoClientUpdateResultresult- Parameters:
collection- the collectionquery- query used to match the documentsreplace- all matching documents will be replaced with thisoptions- options to configure the replace- Returns:
- a future notified with a
MongoClientUpdateResultwhen complete
-
bulkWrite
Execute a bulk operation. Can insert, update, replace, and/or delete multiple documents with one request.- Parameters:
collection- the collectionoperations- the operations to execute- Returns:
- a future notified with a
MongoClientBulkWriteResultwhen complete
-
bulkWriteWithOptions
Future<MongoClientBulkWriteResult> bulkWriteWithOptions(String collection, List<BulkOperation> operations, BulkWriteOptions bulkWriteOptions) Execute a bulk operation with the specified write options. Can insert, update, replace, and/or delete multiple documents with one request.- Parameters:
collection- the collectionoperations- the operations to executebulkWriteOptions- the write options- Returns:
- a future notified with a
MongoClientBulkWriteResultwhen complete
-
find
Find matching documents in the specified collection- Parameters:
collection- the collectionquery- query used to match documents- Returns:
- a future provided with list of documents
-
findBatch
Find matching documents in the specified collection. This method use batchCursor for returning each found document.- Parameters:
collection- the collectionquery- query used to match documents- Returns:
- a
ReadStreamemitting found documents
-
findWithOptions
Find matching documents in the specified collection, specifying options- Parameters:
collection- the collectionquery- query used to match documentsoptions- options to configure the find- Returns:
- a future provided with list of documents
-
findBatchWithOptions
ReadStream<JsonObject> findBatchWithOptions(String collection, JsonObject query, FindOptions options) Find matching documents in the specified collection, specifying options. This method use batchCursor for returning each found document.- Parameters:
collection- the collectionquery- query used to match documentsoptions- options to configure the find- Returns:
- a
ReadStreamemitting found documents
-
findOne
Find a single matching document in the specified collectionThis operation might change _id field of query parameter
- Parameters:
collection- the collectionquery- the query used to match the documentfields- the fields- Returns:
- a future provided with the document, if any
-
findOneAndUpdate
Find a single matching document in the specified collection and update it.This operation might change _id field of query parameter
- Parameters:
collection- the collectionquery- the query used to match the documentupdate- used to describe how the documents will be updated- Returns:
- a future provided with the document, if any
-
findOneAndUpdateWithOptions
Future<JsonObject> findOneAndUpdateWithOptions(String collection, JsonObject query, JsonObject update, FindOptions findOptions, UpdateOptions updateOptions) Find a single matching document in the specified collection and update it.This operation might change _id field of query parameter
- Parameters:
collection- the collectionquery- the query used to match the documentupdate- used to describe how the documents will be updatedfindOptions- options to configure the findupdateOptions- options to configure the update- Returns:
- a future provided with the document, if any
-
findOneAndReplace
Find a single matching document in the specified collection and replace it.This operation might change _id field of query parameter
- Parameters:
collection- the collectionquery- the query used to match the documentreplace- the replacement document- Returns:
- a future provided with the document, if any
-
findOneAndReplaceWithOptions
Future<JsonObject> findOneAndReplaceWithOptions(String collection, JsonObject query, JsonObject replace, FindOptions findOptions, UpdateOptions updateOptions) Find a single matching document in the specified collection and replace it.This operation might change _id field of query parameter
- Parameters:
collection- the collectionquery- the query used to match the documentreplace- the replacement documentfindOptions- options to configure the findupdateOptions- options to configure the update- Returns:
- a future provided with the document, if any
-
findOneAndDelete
Find a single matching document in the specified collection and delete it.This operation might change _id field of query parameter
- Parameters:
collection- the collectionquery- the query used to match the document- Returns:
- a future provided with the deleted document, if any
-
findOneAndDeleteWithOptions
Future<JsonObject> findOneAndDeleteWithOptions(String collection, JsonObject query, FindOptions findOptions) Find a single matching document in the specified collection and delete it.This operation might change _id field of query parameter
- Parameters:
collection- the collectionquery- the query used to match the documentfindOptions- options to configure the find- Returns:
- a future provided with the deleted document, if any
-
count
Count matching documents in a collection.- Parameters:
collection- the collectionquery- query used to match documents- Returns:
- a future provided with the number of matching documents
-
countWithOptions
Count matching documents in a collection.- Parameters:
collection- the collectionquery- query used to match documentscountOptions-- Returns:
- a future provided with the number of matching documents
-
removeDocuments
Remove matching documents from a collection and return the handler withMongoClientDeleteResultresult- Parameters:
collection- the collectionquery- query used to match documents- Returns:
- a future notified with a
MongoClientDeleteResultwhen complete
-
removeDocumentsWithOptions
Future<MongoClientDeleteResult> removeDocumentsWithOptions(String collection, JsonObject query, WriteOption writeOption) Remove matching documents from a collection with the specified write option and return the handler withMongoClientDeleteResultresult- Parameters:
collection- the collectionquery- query used to match documentswriteOption- the write option to use- Returns:
- a future notified with a
MongoClientDeleteResultwhen complete
-
removeDocument
Remove a single matching document from a collection and return the handler withMongoClientDeleteResultresult- Parameters:
collection- the collectionquery- query used to match document- Returns:
- a future notified with a
MongoClientDeleteResultwhen complete
-
removeDocumentWithOptions
Future<MongoClientDeleteResult> removeDocumentWithOptions(String collection, JsonObject query, WriteOption writeOption) Remove a single matching document from a collection with the specified write option and return the handler withMongoClientDeleteResultresult- Parameters:
collection- the collectionquery- query used to match documentwriteOption- the write option to use- Returns:
- a future notified with a
MongoClientDeleteResultwhen complete
-
createCollection
-
createCollectionWithOptions
Future<Void> createCollectionWithOptions(String collectionName, CreateCollectionOptions collectionOptions) Create a new collection with options- Parameters:
collectionName- the name of the collectioncollectionOptions- options of the collection- Returns:
- a future notified once complete
-
getCollections
-
dropCollection
-
renameCollection
-
renameCollectionWithOptions
Future<Void> renameCollectionWithOptions(String oldCollectionName, String newCollectionName, RenameCollectionOptions collectionOptions) Rename a collection- Parameters:
oldCollectionName- the name of the collectionnewCollectionName- the new name of the collectioncollectionOptions- options of the collection- Returns:
- a future notified when complete
-
createIndex
Creates an index.- Parameters:
collection- the collectionkey- A document that contains the field and value pairs where the field is the index key and the value describes the type of index for that field. For an ascending index on a field, specify a value of 1; for descending index, specify a value of -1.- Returns:
- a future notified when complete
-
createIndexWithOptions
Creates an index.- Parameters:
collection- the collectionkey- A document that contains the field and value pairs where the field is the index key and the value describes the type of index for that field. For an ascending index on a field, specify a value of 1; for descending index, specify a value of -1.options- the options for the index- Returns:
- a future notified when complete
-
createIndexes
creates an indexes- Parameters:
collection- the collectionindexes- A model that contains pairs of document and indexOptions, document contains the field and value pairs where the field is the index key and the value describes the type of index for that field. For an ascending index on a field, specify a value of 1; for descending index, specify a value of -1.- Returns:
- a future notified when complete
-
listIndexes
-
dropIndex
-
dropIndex
Drops the index given the keys used to create it.- Parameters:
collection- the collectionkey- the key(s) of the index to remove- Returns:
- a future notified when complete
-
runCommand
Run an arbitrary MongoDB command.- Parameters:
commandName- the name of the commandcommand- the command- Returns:
- a future notified with the result.
-
distinct
Gets the distinct values of the specified field name. Return a JsonArray containing distinct values (eg: [ 1 , 89 ])- Parameters:
collection- the collectionfieldName- the field name- Returns:
- a future provided with array of values.
-
distinct
Future<JsonArray> distinct(String collection, String fieldName, String resultClassname, DistinctOptions distinctOptions) Gets the distinct values of the specified field name. Return a JsonArray containing distinct values (eg: [ 1 , 89 ])- Parameters:
collection- the collectionfieldName- the field namedistinctOptions- options (e.g. collation)- Returns:
- a future provided with array of values.
-
distinctWithQuery
Future<JsonArray> distinctWithQuery(String collection, String fieldName, String resultClassname, JsonObject query) Gets the distinct values of the specified field name filtered by specified query. Return a JsonArray containing distinct values (eg: [ 1 , 89 ])- Parameters:
collection- the collectionfieldName- the field namequery- the query- Returns:
- a future provided with array of values.
-
distinctWithQuery
Future<JsonArray> distinctWithQuery(String collection, String fieldName, String resultClassname, JsonObject query, DistinctOptions distinctOptions) Gets the distinct values of the specified field name filtered by specified query. Return a JsonArray containing distinct values (eg: [ 1 , 89 ])- Parameters:
collection- the collectionfieldName- the field namequery- the querydistinctOptions- options (e.g. collation)- Returns:
- a future provided with array of values.
-
distinctBatch
Gets the distinct values of the specified field name. This method use batchCursor for returning each found value. Each value is a json fragment with fieldName key (eg: {"num": 1}).- Parameters:
collection- the collectionfieldName- the field name- Returns:
- a
ReadStreamemitting json fragments
-
distinctBatch
ReadStream<JsonObject> distinctBatch(String collection, String fieldName, String resultClassname, DistinctOptions distinctOptions) Gets the distinct values of the specified field name. This method use batchCursor for returning each found value. Each value is a json fragment with fieldName key (eg: {"num": 1}).- Parameters:
collection- the collectionfieldName- the field namedistinctOptions- options (e.g. collation)- Returns:
- a
ReadStreamemitting json fragments
-
distinctBatchWithQuery
ReadStream<JsonObject> distinctBatchWithQuery(String collection, String fieldName, String resultClassname, JsonObject query) Gets the distinct values of the specified field name filtered by specified query. This method use batchCursor for returning each found value. Each value is a json fragment with fieldName key (eg: {"num": 1}).- Parameters:
collection- the collectionfieldName- the field namequery- the query- Returns:
- a
ReadStreamemitting json fragments
-
distinctBatchWithQuery
ReadStream<JsonObject> distinctBatchWithQuery(String collection, String fieldName, String resultClassname, JsonObject query, DistinctOptions distinctOptions) Gets the distinct values of the specified field name filtered by specified query. This method use batchCursor for returning each found value. Each value is a json fragment with fieldName key (eg: {"num": 1}).- Parameters:
collection- the collectionfieldName- the field namequery- the querydistinctOptions- options (e.g. collation)- Returns:
- a
ReadStreamemitting json fragments
-
distinctBatchWithQuery
ReadStream<JsonObject> distinctBatchWithQuery(String collection, String fieldName, String resultClassname, JsonObject query, int batchSize) Gets the distinct values of the specified field name filtered by specified query. This method use batchCursor for returning each found value. Each value is a json fragment with fieldName key (eg: {"num": 1}).- Parameters:
collection- the collectionfieldName- the field namequery- the querybatchSize- the number of documents to load in a batch- Returns:
- a
ReadStreamemitting json fragments
-
distinctBatchWithQuery
ReadStream<JsonObject> distinctBatchWithQuery(String collection, String fieldName, String resultClassname, JsonObject query, int batchSize, DistinctOptions distinctOptions) Gets the distinct values of the specified field name filtered by specified query. This method use batchCursor for returning each found value. Each value is a json fragment with fieldName key (eg: {"num": 1}).- Parameters:
collection- the collectionfieldName- the field namequery- the querybatchSize- the number of documents to load in a batchdistinctOptions- options (e.g. collation)- Returns:
- a
ReadStreamemitting json fragments
-
aggregate
Run aggregate MongoDB command with defaultAggregateOptions.- Parameters:
collection- the collectionpipeline- aggregation pipeline to be executed
-
aggregateWithOptions
ReadStream<JsonObject> aggregateWithOptions(String collection, JsonArray pipeline, AggregateOptions options) Run aggregate MongoDB command.- Parameters:
collection- the collectionpipeline- aggregation pipeline to be executedoptions- options to configure the aggregation command
-
watch
ReadStream<com.mongodb.client.model.changestream.ChangeStreamDocument<JsonObject>> watch(String collection, JsonArray pipeline, boolean withUpdatedDoc, int batchSize) Watch the collection change.- Parameters:
collection- the collectionpipeline- watching pipeline to be executedwithUpdatedDoc- whether to get updated fullDocument for "update" operationbatchSize- the number of documents to load in a batch
-
createDefaultGridFsBucketService
Future<MongoGridFsClient> createDefaultGridFsBucketService()Creates aMongoGridFsClientused to interact with Mongo GridFS.- Returns:
- a future notified with the
MongoGridFsClientto interact with the bucket named bucketName
-
createGridFsBucketService
Creates aMongoGridFsClientused to interact with Mongo GridFS.- Parameters:
bucketName- the name of the GridFS bucket- Returns:
- a future notified with the
MongoGridFsClientto interact with the bucket named bucketName
-
close
-