Class MongoDatabase
java.lang.Object
dev.magicmq.pyspigot.manager.database.Database
dev.magicmq.pyspigot.manager.database.mongo.MongoDatabase
Represents an open connection to a Mongo Database.
Note: Most methods in this class should be called from scripts only!
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanclose()Closes a connection to the database.booleancreateCollection(String database, String collection) Create a collection in the given databasecreateCollectionIndex(String database, String collection, Bson keys) Create a collection with an index of the given keys.Create an emptyDocument.createDocument(String json) Create aDocumentout of the provided json.createDocument(String key, Object value) Create aDocumentwith the provided key and value.Create a new emptyBasicDBObject.createObject(String json) Create a newBasicDBObjectout of the provided json.createObject(String key, Object value) Create a newBasicDBObjectwith the provided key and value.booleandeleteCollection(String database, String collection) Delete a collection in the given databasedeleteDocument(String database, String collection, Bson filter) Delete a document from a collection matching the provided filter.deleteDocuments(String database, String collection, Bson filter) Delete multiple documents from a collection matching the provided filter.booleandoesCollectionExist(String database, String collection) Check if a collection exists within a database.booleandoesDatabaseExist(String database) Check if a database exists with the given name.Fetch a newFindOneAndUpdateOptionsobject for updatesFetch a newUpdateOptionsobject for updatesUpdate and return a document within a collection that match the given filter, with the default update optionsfindAndUpdateDocument(String database, String collection, Bson filter, List<Bson> update, FindOneAndUpdateOptions updateOptions) Update and return a document within a collection that match the given filter, with the default update optionsfindAndUpdateDocument(String database, String collection, Bson filter, Bson update) Update and return a document within a collection that match the given filter, with the default update optionsfindAndUpdateDocument(String database, String collection, Bson filter, Bson update, FindOneAndUpdateOptions updateOptions) Update and return a document within a collection that match the given filter, with the default update optionsgetCollection(String database, String collection) Get a collection from a database.getCollectionNames(String database) Get all collection names within a database.getCollections(String database) Get all collections within a database.getDatabase(String database) Get aMongoDatabasewith the given name.Get all database names.Get all databases.getDocument(String database, String collection, Bson filter) Get a document within a collection that match the given filter.Get a document within a collection that match the given filter, projections, and sort criteria.getDocuments(String database, String collection) Get all documents within a collection.getDocuments(String database, String collection, Bson filter) Get all documents within a collection that match the given filter.Get theMongoClientassociated with this Mongo Database connection.insertDocument(String database, String collection, Document document) Insert a document into a collection.insertDocuments(String database, String collection, List<Document> documents) Insert multiple documents into a collection.booleanopen()Opens a connection to the database.toString()Prints a representation of this MongoDatabase in string format, including the URI andMongoClientUpdate one or more documents within a collection that match the given filter, with the default update optionsupdateDocument(String database, String collection, Bson filter, List<Bson> updates, UpdateOptions updateOptions) Update one or more documents within a collection that match the given filter, with the provided update optionsupdateDocument(String database, String collection, Bson filter, Bson update) Update one or more documents within a collection that match the given filter, with the default update optionsupdateDocument(String database, String collection, Bson filter, Bson update, UpdateOptions updateOptions) Update one or more documents within a collection that match the given filter, with the provided update optionsUpdate multiple documents within a collection.updateDocuments(String database, String collection, Bson filter, List<Bson> update, UpdateOptions updateOptions) Update multiple documents within a collection.updateDocuments(String database, String collection, Bson filter, Bson update) Update multiple documents within a collection.updateDocuments(String database, String collection, Bson filter, Bson update, UpdateOptions updateOptions) Update multiple documents within a collection.Methods inherited from class dev.magicmq.pyspigot.manager.database.Database
getDatabaseId, getScript
-
Constructor Details
-
MongoDatabase
- Parameters:
script- The script associated with this MongoDatabaseclientSettings- The client settings for the MongoDatabase connection
-
-
Method Details
-
open
public boolean open()Opens a connection to the database. -
close
public boolean close()Closes a connection to the database. -
getMongoClient
Get theMongoClientassociated with this Mongo Database connection.- Returns:
- The MongoClient
-
fetchNewUpdateOptions
Fetch a newUpdateOptionsobject for updates -
fetchNewFindOneAndUpdateOptions
Fetch a newFindOneAndUpdateOptionsobject for updates -
createObject
Create a new emptyBasicDBObject.- Returns:
- The BasicDBObject
-
createObject
Create a newBasicDBObjectout of the provided json.- Returns:
- The BasicDBObject
-
createObject
Create a newBasicDBObjectwith the provided key and value.- Returns:
- The BasicDBObject
-
createDocument
Create an emptyDocument.- Returns:
- The document
-
createDocument
Create aDocumentout of the provided json.- Parameters:
json- A JSON representation of the document- Returns:
- The document
-
createDocument
Create aDocumentwith the provided key and value.- Parameters:
key- The keyvalue- The value- Returns:
- The document
-
getDatabase
Get aMongoDatabasewith the given name.- Parameters:
database- The name of the database- Returns:
- The database
-
getDatabaseNames
Get all database names.- Returns:
- An iterable list of type
MongoIterable<String>containing all database names
-
getDatabases
Get all databases.- Returns:
- An iterable list of type
MongoIterable<Document>containing all databases
-
doesDatabaseExist
Check if a database exists with the given name.- Parameters:
database- The name of the database to check- Returns:
- True if the database exists, false if otherwise
-
createCollection
Create a collection in the given database- Parameters:
database- The name of the database where the collection should be createdcollection- The name for the collection- Returns:
- True if the collection was created, false if it already exists in the database
-
deleteCollection
Delete a collection in the given database- Parameters:
database- The name of the database where the collection should be deletedcollection- The name of the collection- Returns:
- True if the collection was deleted, false if it did not exist in the database
-
getCollection
Get a collection from a database.- Parameters:
database- The name of the database to fetch fromcollection- The name of the collection to get- Returns:
- A
MongoCollection<Document>containingDocumentrepresenting the collection
-
getCollectionNames
Get all collection names within a database.- Parameters:
database- The database to get collections names from- Returns:
- An iterable list of type
ListCollectionNamesIterablecontaining all collection names
-
getCollections
Get all collections within a database.- Parameters:
database- The database to get collections from- Returns:
- An iterable list of type
ListCollectionsIterable<Document>containing all collections
-
doesCollectionExist
Check if a collection exists within a database.- Parameters:
database- The name of the database to checkcollection- The name of the collection to check- Returns:
- True if the collection exists, false if otherwise
-
createCollectionIndex
Create a collection with an index of the given keys.- Parameters:
database- The name of the database where the collection should be createdcollection- The name for the collectionkeys- ABsonobject representing the index keys- Returns:
-
getDocument
Get a document within a collection that match the given filter.- Parameters:
database- The name of the database to fetch fromcollection- The name of the collection to fetch fromfilter- ABsonobject representing a filter to filter documents within the collection- Returns:
- The first document within the collection that matched the provided filter
-
getDocument
public Document getDocument(String database, String collection, Bson filter, Bson projections, Bson sorts) Get a document within a collection that match the given filter, projections, and sort criteria.- Parameters:
database- The name of the database to fetch fromcollection- The name of the collection to fetch fromfilter- ABsonobject representing a filter to filter documents within the collectionprojections- The project documentsorts- Sort criteria- Returns:
- The first document within the collection that matched the provided filter, projections, and sort criteria
-
getDocuments
Get all documents within a collection.- Parameters:
database- The name of the database to fetch fromcollection- The name of the collection to fetch from- Returns:
- An iterable list of type
FindIterable<Document>containing all documents within the collection
-
getDocuments
Get all documents within a collection that match the given filter.- Parameters:
database- The name of the database to fetch fromcollection- The name of the collection to fetch fromfilter- ABsonobject representing a filter to filter documents within the collection- Returns:
- An iterable list of type
FindIterable<Document>containing all documents within the collection that matched the provided filter
-
insertDocument
Insert a document into a collection.- Parameters:
database- The database that contains the collectioncollection- The collection to insert intodocument- The document to insert- Returns:
- An
InsertOneResultrepresenting the outcome of the operation
-
insertDocuments
public InsertManyResult insertDocuments(String database, String collection, List<Document> documents) Insert multiple documents into a collection.- Parameters:
database- The database that contains the collectioncollection- The collection to insert intodocuments- The documents to insert- Returns:
- An
InsertManyResultrepresenting the outcome of the operation
-
updateDocument
Update one or more documents within a collection that match the given filter, with the default update options- Parameters:
database- The database that contains the collectioncollection- The collection containing the documentfilter- ABsonobject representing a filter to filter documents within the collectionupdate- The update that should be applied to the first matching document- Returns:
- An
UpdateResultrepresenting the outcome of the operation
-
updateDocument
public UpdateResult updateDocument(String database, String collection, Bson filter, Bson update, UpdateOptions updateOptions) Update one or more documents within a collection that match the given filter, with the provided update options- Parameters:
database- The database that contains the collectioncollection- The collection containing the documentfilter- ABsonobject representing a filter to filter documents within the collectionupdate- The update that that should be applied to the first matching documentupdateOptions- AUpdateOptionsobject representing the options to apply to the update operation- Returns:
- An
UpdateResultrepresenting the outcome of the operation
-
updateDocument
public UpdateResult updateDocument(String database, String collection, Bson filter, List<Bson> updates) Update one or more documents within a collection that match the given filter, with the default update options- Parameters:
database- The database that contains the collectioncollection- The collection containing the documentfilter- ABsonobject representing a filter to filter documents within the collectionupdates- The updates that should be applied to the first matching document- Returns:
- An
UpdateResultrepresenting the outcome of the operation
-
updateDocument
public UpdateResult updateDocument(String database, String collection, Bson filter, List<Bson> updates, UpdateOptions updateOptions) Update one or more documents within a collection that match the given filter, with the provided update options- Parameters:
database- The database that contains the collectioncollection- The collection containing the documentfilter- ABsonobject representing a filter to filter documents within the collectionupdates- The updates that should be applied to the first matching documentupdateOptions- AUpdateOptionsobject representing the options to apply to the update operation- Returns:
- An
UpdateResultrepresenting the outcome of the operation
-
findAndUpdateDocument
Update and return a document within a collection that match the given filter, with the default update options- Parameters:
database- The database that contains the collectioncollection- The collection containing the documentfilter- ABsonobject representing a filter to filter documents within the collectionupdate- The update that should be applied to the first matching document- Returns:
- An
UpdateResultrepresenting the outcome of the operation
-
findAndUpdateDocument
public Document findAndUpdateDocument(String database, String collection, Bson filter, Bson update, FindOneAndUpdateOptions updateOptions) Update and return a document within a collection that match the given filter, with the default update options- Parameters:
database- The database that contains the collectioncollection- The collection containing the documentfilter- ABsonobject representing a filter to filter documents within the collectionupdate- The update that should be applied to the first matching documentupdateOptions- AFindOneAndUpdateOptionsobject representing the options to apply to the update operation- Returns:
- The document that was updated
-
findAndUpdateDocument
public Document findAndUpdateDocument(String database, String collection, Bson filter, List<Bson> update) Update and return a document within a collection that match the given filter, with the default update options- Parameters:
database- The database that contains the collectioncollection- The collection containing the documentfilter- ABsonobject representing a filter to filter documents within the collectionupdate- The updates that should be applied to the first matching document- Returns:
- The document that was updated
-
findAndUpdateDocument
public Document findAndUpdateDocument(String database, String collection, Bson filter, List<Bson> update, FindOneAndUpdateOptions updateOptions) Update and return a document within a collection that match the given filter, with the default update options- Parameters:
database- The database that contains the collectioncollection- The collection containing the documentfilter- ABsonobject representing a filter to filter documents within the collectionupdate- The updates that should be applied to the first matching documentupdateOptions- AFindOneAndUpdateOptionsobject representing the options to apply to the update operation- Returns:
- The document that was updated
-
updateDocuments
Update multiple documents within a collection.- Parameters:
database- The database that contains the collectioncollection- The collection whose documents should be updatedfilter- ABsonobject representing a filter to filter documents within the collectionupdate- The update that should be applied to all matching documents- Returns:
- An
UpdateResultrepresenting the outcome of the operation
-
updateDocuments
public UpdateResult updateDocuments(String database, String collection, Bson filter, Bson update, UpdateOptions updateOptions) Update multiple documents within a collection.- Parameters:
database- The database that contains the collectioncollection- The collection whose documents should be updatedfilter- ABsonobject representing a filter to filter documents within the collectionupdate- The update that should be applied to all matching documentsupdateOptions- AUpdateOptionsobject representing the options to apply to the update operation- Returns:
- An
UpdateResultrepresenting the outcome of the operation
-
updateDocuments
public UpdateResult updateDocuments(String database, String collection, Bson filter, List<Bson> update) Update multiple documents within a collection.- Parameters:
database- The database that contains the collectioncollection- The collection whose documents should be updatedfilter- ABsonobject representing a filter to filter documents within the collectionupdate- The updates that should be applied to all matching documents- Returns:
- An
UpdateResultrepresenting the outcome of the operation
-
updateDocuments
public UpdateResult updateDocuments(String database, String collection, Bson filter, List<Bson> update, UpdateOptions updateOptions) Update multiple documents within a collection.- Parameters:
database- The database that contains the collectioncollection- The collection whose documents should be updatedfilter- ABsonobject representing a filter to filter documents within the collectionupdate- The updates that should be applied to all matching documentsupdateOptions- AUpdateOptionsobject representing the options to apply to the update operation- Returns:
- An
UpdateResultrepresenting the outcome of the operation
-
deleteDocument
Delete a document from a collection matching the provided filter.- Parameters:
database- The database that contains the collectioncollection- The collection that contains the documentfilter- ABsonobject representing a filter to filter documents within the collection- Returns:
- An
DeleteResultrepresenting the outcome of the operation
-
deleteDocuments
Delete multiple documents from a collection matching the provided filter.- Parameters:
database- The database that contains the collectioncollection- The collection that contains the documentsfilter- ABsonobject representing a filter to filter documents within the collection- Returns:
- An
DeleteResultrepresenting the outcome of the operation
-
toString
Prints a representation of this MongoDatabase in string format, including the URI andMongoClient
-