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
ConstructorDescriptionMongoDatabase
(Script script, com.mongodb.MongoClientSettings clientSettings) -
Method Summary
Modifier and TypeMethodDescriptionboolean
close()
Closes a connection to the database.boolean
createCollection
(String database, String collection) Create a collection in the given databasecreateCollectionIndex
(String database, String collection, org.bson.conversions.Bson keys) Create a collection with an index of the given keys.org.bson.Document
Create an emptyDocument
.org.bson.Document
createDocument
(String json) Create aDocument
out of the provided json.org.bson.Document
createDocument
(String key, Object value) Create aDocument
with the provided key and value.com.mongodb.BasicDBObject
Create a new emptyBasicDBObject
.com.mongodb.BasicDBObject
createObject
(String json) Create a newBasicDBObject
out of the provided json.com.mongodb.BasicDBObject
createObject
(String key, Object value) Create a newBasicDBObject
with the provided key and value.boolean
deleteCollection
(String database, String collection) Delete a collection in the given databasecom.mongodb.client.result.DeleteResult
deleteDocument
(String database, String collection, org.bson.conversions.Bson filter) Delete a document from a collection matching the provided filter.com.mongodb.client.result.DeleteResult
deleteDocuments
(String database, String collection, org.bson.conversions.Bson filter) Delete multiple documents from a collection matching the provided filter.boolean
doesCollectionExist
(String database, String collection) Check if a collection exists within a database.boolean
doesDatabaseExist
(String database) Check if a database exists with the given name.com.mongodb.client.model.FindOneAndUpdateOptions
Fetch a newFindOneAndUpdateOptions
object for updatescom.mongodb.client.model.UpdateOptions
Fetch a newUpdateOptions
object for updatesorg.bson.Document
findAndUpdateDocument
(String database, String collection, org.bson.conversions.Bson filter, List<org.bson.conversions.Bson> update) Update and return a document within a collection that match the given filter, with the default update optionsorg.bson.Document
findAndUpdateDocument
(String database, String collection, org.bson.conversions.Bson filter, List<org.bson.conversions.Bson> update, com.mongodb.client.model.FindOneAndUpdateOptions updateOptions) Update and return a document within a collection that match the given filter, with the default update optionsorg.bson.Document
findAndUpdateDocument
(String database, String collection, org.bson.conversions.Bson filter, org.bson.conversions.Bson update) Update and return a document within a collection that match the given filter, with the default update optionsorg.bson.Document
findAndUpdateDocument
(String database, String collection, org.bson.conversions.Bson filter, org.bson.conversions.Bson update, com.mongodb.client.model.FindOneAndUpdateOptions updateOptions) Update and return a document within a collection that match the given filter, with the default update optionsMongoCollection<org.bson.Document>
getCollection
(String database, String collection) Get a collection from a database.getCollectionNames
(String database) Get all collection names within a database.ListCollectionsIterable<org.bson.Document>
getCollections
(String database) Get all collections within a database.getDatabase
(String database) Get aMongoDatabase
with the given name.Get all database names.MongoIterable<org.bson.Document>
Get all databases.org.bson.Document
getDocument
(String database, String collection, org.bson.conversions.Bson filter) Get a document within a collection that match the given filter.org.bson.Document
getDocument
(String database, String collection, org.bson.conversions.Bson filter, org.bson.conversions.Bson projections, org.bson.conversions.Bson sorts) Get a document within a collection that match the given filter, projections, and sort criteria.FindIterable<org.bson.Document>
getDocuments
(String database, String collection) Get all documents within a collection.FindIterable<org.bson.Document>
getDocuments
(String database, String collection, org.bson.conversions.Bson filter) Get all documents within a collection that match the given filter.Get theMongoClient
associated with this Mongo Database connection.com.mongodb.client.result.InsertOneResult
insertDocument
(String database, String collection, org.bson.Document document) Insert a document into a collection.com.mongodb.client.result.InsertManyResult
insertDocuments
(String database, String collection, List<org.bson.Document> documents) Insert multiple documents into a collection.boolean
open()
Opens a connection to the database.toString()
Prints a representation of this MongoDatabase in string format, including the URI andMongoClient
com.mongodb.client.result.UpdateResult
updateDocument
(String database, String collection, org.bson.conversions.Bson filter, List<org.bson.conversions.Bson> updates) Update one or more documents within a collection that match the given filter, with the default update optionscom.mongodb.client.result.UpdateResult
updateDocument
(String database, String collection, org.bson.conversions.Bson filter, List<org.bson.conversions.Bson> updates, com.mongodb.client.model.UpdateOptions updateOptions) Update one or more documents within a collection that match the given filter, with the provided update optionscom.mongodb.client.result.UpdateResult
updateDocument
(String database, String collection, org.bson.conversions.Bson filter, org.bson.conversions.Bson update) Update one or more documents within a collection that match the given filter, with the default update optionscom.mongodb.client.result.UpdateResult
updateDocument
(String database, String collection, org.bson.conversions.Bson filter, org.bson.conversions.Bson update, com.mongodb.client.model.UpdateOptions updateOptions) Update one or more documents within a collection that match the given filter, with the provided update optionscom.mongodb.client.result.UpdateResult
updateDocuments
(String database, String collection, org.bson.conversions.Bson filter, List<org.bson.conversions.Bson> update) Update multiple documents within a collection.com.mongodb.client.result.UpdateResult
updateDocuments
(String database, String collection, org.bson.conversions.Bson filter, List<org.bson.conversions.Bson> update, com.mongodb.client.model.UpdateOptions updateOptions) Update multiple documents within a collection.com.mongodb.client.result.UpdateResult
updateDocuments
(String database, String collection, org.bson.conversions.Bson filter, org.bson.conversions.Bson update) Update multiple documents within a collection.com.mongodb.client.result.UpdateResult
updateDocuments
(String database, String collection, org.bson.conversions.Bson filter, org.bson.conversions.Bson update, com.mongodb.client.model.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 theMongoClient
associated with this Mongo Database connection.- Returns:
- The MongoClient
-
fetchNewUpdateOptions
public com.mongodb.client.model.UpdateOptions fetchNewUpdateOptions()Fetch a newUpdateOptions
object for updates -
fetchNewFindOneAndUpdateOptions
public com.mongodb.client.model.FindOneAndUpdateOptions fetchNewFindOneAndUpdateOptions()Fetch a newFindOneAndUpdateOptions
object for updates -
createObject
public com.mongodb.BasicDBObject createObject()Create a new emptyBasicDBObject
.- Returns:
- The BasicDBObject
-
createObject
Create a newBasicDBObject
out of the provided json.- Returns:
- The BasicDBObject
-
createObject
Create a newBasicDBObject
with the provided key and value.- Returns:
- The BasicDBObject
-
createDocument
public org.bson.Document createDocument()Create an emptyDocument
.- Returns:
- The document
-
createDocument
Create aDocument
out of the provided json.- Parameters:
json
- A JSON representation of the document- Returns:
- The document
-
createDocument
Create aDocument
with the provided key and value.- Parameters:
key
- The keyvalue
- The value- Returns:
- The document
-
getDatabase
Get aMongoDatabase
with 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<org.bson.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<org.bson.Document>
containingDocument
representing 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
ListCollectionNamesIterable
containing 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<org.bson.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
public String createCollectionIndex(String database, String collection, org.bson.conversions.Bson keys) 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
- ABson
object representing the index keys- Returns:
-
getDocument
public org.bson.Document getDocument(String database, String collection, org.bson.conversions.Bson filter) 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
- ABson
object representing a filter to filter documents within the collection- Returns:
- The first document within the collection that matched the provided filter
-
getDocument
public org.bson.Document getDocument(String database, String collection, org.bson.conversions.Bson filter, org.bson.conversions.Bson projections, org.bson.conversions.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
- ABson
object 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<org.bson.Document>
containing all documents within the collection
-
getDocuments
public FindIterable<org.bson.Document> getDocuments(String database, String collection, org.bson.conversions.Bson filter) 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
- ABson
object representing a filter to filter documents within the collection- Returns:
- An iterable list of type
FindIterable<org.bson.Document>
containing all documents within the collection that matched the provided filter
-
insertDocument
public com.mongodb.client.result.InsertOneResult insertDocument(String database, String collection, org.bson.Document document) 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
InsertOneResult
representing the outcome of the operation
-
insertDocuments
public com.mongodb.client.result.InsertManyResult insertDocuments(String database, String collection, List<org.bson.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
InsertManyResult
representing the outcome of the operation
-
updateDocument
public com.mongodb.client.result.UpdateResult updateDocument(String database, String collection, org.bson.conversions.Bson filter, org.bson.conversions.Bson update) 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
- ABson
object representing a filter to filter documents within the collectionupdate
- The update that should be applied to the first matching document- Returns:
- An
UpdateResult
representing the outcome of the operation
-
updateDocument
public com.mongodb.client.result.UpdateResult updateDocument(String database, String collection, org.bson.conversions.Bson filter, org.bson.conversions.Bson update, com.mongodb.client.model.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
- ABson
object representing a filter to filter documents within the collectionupdate
- The update that that should be applied to the first matching documentupdateOptions
- AUpdateOptions
object representing the options to apply to the update operation- Returns:
- An
UpdateResult
representing the outcome of the operation
-
updateDocument
public com.mongodb.client.result.UpdateResult updateDocument(String database, String collection, org.bson.conversions.Bson filter, List<org.bson.conversions.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
- ABson
object representing a filter to filter documents within the collectionupdates
- The updates that should be applied to the first matching document- Returns:
- An
UpdateResult
representing the outcome of the operation
-
updateDocument
public com.mongodb.client.result.UpdateResult updateDocument(String database, String collection, org.bson.conversions.Bson filter, List<org.bson.conversions.Bson> updates, com.mongodb.client.model.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
- ABson
object representing a filter to filter documents within the collectionupdates
- The updates that should be applied to the first matching documentupdateOptions
- AUpdateOptions
object representing the options to apply to the update operation- Returns:
- An
UpdateResult
representing the outcome of the operation
-
findAndUpdateDocument
public org.bson.Document findAndUpdateDocument(String database, String collection, org.bson.conversions.Bson filter, org.bson.conversions.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
- ABson
object representing a filter to filter documents within the collectionupdate
- The update that should be applied to the first matching document- Returns:
- An
UpdateResult
representing the outcome of the operation
-
findAndUpdateDocument
public org.bson.Document findAndUpdateDocument(String database, String collection, org.bson.conversions.Bson filter, org.bson.conversions.Bson update, com.mongodb.client.model.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
- ABson
object representing a filter to filter documents within the collectionupdate
- The update that should be applied to the first matching documentupdateOptions
- AFindOneAndUpdateOptions
object representing the options to apply to the update operation- Returns:
- The document that was updated
-
findAndUpdateDocument
public org.bson.Document findAndUpdateDocument(String database, String collection, org.bson.conversions.Bson filter, List<org.bson.conversions.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
- ABson
object 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 org.bson.Document findAndUpdateDocument(String database, String collection, org.bson.conversions.Bson filter, List<org.bson.conversions.Bson> update, com.mongodb.client.model.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
- ABson
object representing a filter to filter documents within the collectionupdate
- The updates that should be applied to the first matching documentupdateOptions
- AFindOneAndUpdateOptions
object representing the options to apply to the update operation- Returns:
- The document that was updated
-
updateDocuments
public com.mongodb.client.result.UpdateResult updateDocuments(String database, String collection, org.bson.conversions.Bson filter, org.bson.conversions.Bson update) Update multiple documents within a collection.- Parameters:
database
- The database that contains the collectioncollection
- The collection whose documents should be updatedfilter
- ABson
object representing a filter to filter documents within the collectionupdate
- The update that should be applied to all matching documents- Returns:
- An
UpdateResult
representing the outcome of the operation
-
updateDocuments
public com.mongodb.client.result.UpdateResult updateDocuments(String database, String collection, org.bson.conversions.Bson filter, org.bson.conversions.Bson update, com.mongodb.client.model.UpdateOptions updateOptions) Update multiple documents within a collection.- Parameters:
database
- The database that contains the collectioncollection
- The collection whose documents should be updatedfilter
- ABson
object representing a filter to filter documents within the collectionupdate
- The update that should be applied to all matching documentsupdateOptions
- AUpdateOptions
object representing the options to apply to the update operation- Returns:
- An
UpdateResult
representing the outcome of the operation
-
updateDocuments
public com.mongodb.client.result.UpdateResult updateDocuments(String database, String collection, org.bson.conversions.Bson filter, List<org.bson.conversions.Bson> update) Update multiple documents within a collection.- Parameters:
database
- The database that contains the collectioncollection
- The collection whose documents should be updatedfilter
- ABson
object representing a filter to filter documents within the collectionupdate
- The updates that should be applied to all matching documents- Returns:
- An
UpdateResult
representing the outcome of the operation
-
updateDocuments
public com.mongodb.client.result.UpdateResult updateDocuments(String database, String collection, org.bson.conversions.Bson filter, List<org.bson.conversions.Bson> update, com.mongodb.client.model.UpdateOptions updateOptions) Update multiple documents within a collection.- Parameters:
database
- The database that contains the collectioncollection
- The collection whose documents should be updatedfilter
- ABson
object representing a filter to filter documents within the collectionupdate
- The updates that should be applied to all matching documentsupdateOptions
- AUpdateOptions
object representing the options to apply to the update operation- Returns:
- An
UpdateResult
representing the outcome of the operation
-
deleteDocument
public com.mongodb.client.result.DeleteResult deleteDocument(String database, String collection, org.bson.conversions.Bson filter) Delete a document from a collection matching the provided filter.- Parameters:
database
- The database that contains the collectioncollection
- The collection that contains the documentfilter
- ABson
object representing a filter to filter documents within the collection- Returns:
- An
DeleteResult
representing the outcome of the operation
-
deleteDocuments
public com.mongodb.client.result.DeleteResult deleteDocuments(String database, String collection, org.bson.conversions.Bson filter) Delete multiple documents from a collection matching the provided filter.- Parameters:
database
- The database that contains the collectioncollection
- The collection that contains the documentsfilter
- ABson
object representing a filter to filter documents within the collection- Returns:
- An
DeleteResult
representing the outcome of the operation
-
toString
Prints a representation of this MongoDatabase in string format, including the URI andMongoClient
-