Class MongoDatabase

java.lang.Object
dev.magicmq.pyspigot.manager.database.Database
dev.magicmq.pyspigot.manager.database.mongo.MongoDatabase

public class MongoDatabase extends Database
Represents an open connection to a Mongo Database.

Note: Most methods in this class should be called from scripts only!

  • Constructor Summary

    Constructors
    Constructor
    Description
    MongoDatabase(Script script, com.mongodb.MongoClientSettings clientSettings)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Closes a connection to the database.
    boolean
    createCollection(String database, String collection)
    Create a collection in the given database
    createCollectionIndex(String database, String collection, org.bson.conversions.Bson keys)
    Create a collection with an index of the given keys.
    org.bson.Document
    Create an empty Document.
    org.bson.Document
    Create a Document out of the provided json.
    org.bson.Document
    Create a Document with the provided key and value.
    com.mongodb.BasicDBObject
    Create a new empty BasicDBObject.
    com.mongodb.BasicDBObject
    Create a new BasicDBObject out of the provided json.
    com.mongodb.BasicDBObject
    createObject(String key, Object value)
    Create a new BasicDBObject with the provided key and value.
    boolean
    deleteCollection(String database, String collection)
    Delete a collection in the given database
    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.
    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
    Check if a database exists with the given name.
    com.mongodb.client.model.FindOneAndUpdateOptions
    Fetch a new FindOneAndUpdateOptions object for updates
    com.mongodb.client.model.UpdateOptions
    Fetch a new UpdateOptions object for updates
    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
    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
    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
    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
    MongoCollection<org.bson.Document>
    getCollection(String database, String collection)
    Get a collection from a database.
    Get all collection names within a database.
    ListCollectionsIterable<org.bson.Document>
    Get all collections within a database.
    getDatabase(String database)
    Get a MongoDatabase 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 the MongoClient 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
    Opens a connection to the database.
    Prints a representation of this MongoDatabase in string format, including the URI and MongoClient
    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
    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
    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
    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
    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.
    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

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • MongoDatabase

      public MongoDatabase(Script script, com.mongodb.MongoClientSettings clientSettings)
      Parameters:
      script - The script associated with this MongoDatabase
      clientSettings - The client settings for the MongoDatabase connection
  • Method Details

    • open

      public boolean open()
      Opens a connection to the database.
      Specified by:
      open in class Database
      Returns:
      True if opening the connection to the database was successful, false if otherwise
    • close

      public boolean close()
      Closes a connection to the database.
      Specified by:
      close in class Database
      Returns:
      True if closing the connection to the database was successful, false if otherwise
    • getMongoClient

      public MongoClient getMongoClient()
      Get the MongoClient associated with this Mongo Database connection.
      Returns:
      The MongoClient
    • fetchNewUpdateOptions

      public com.mongodb.client.model.UpdateOptions fetchNewUpdateOptions()
      Fetch a new UpdateOptions object for updates
    • fetchNewFindOneAndUpdateOptions

      public com.mongodb.client.model.FindOneAndUpdateOptions fetchNewFindOneAndUpdateOptions()
      Fetch a new FindOneAndUpdateOptions object for updates
    • createObject

      public com.mongodb.BasicDBObject createObject()
      Create a new empty BasicDBObject.
      Returns:
      The BasicDBObject
    • createObject

      public com.mongodb.BasicDBObject createObject(String json)
      Create a new BasicDBObject out of the provided json.
      Returns:
      The BasicDBObject
    • createObject

      public com.mongodb.BasicDBObject createObject(String key, Object value)
      Create a new BasicDBObject with the provided key and value.
      Returns:
      The BasicDBObject
    • createDocument

      public org.bson.Document createDocument()
      Create an empty Document.
      Returns:
      The document
    • createDocument

      public org.bson.Document createDocument(String json)
      Create a Document out of the provided json.
      Parameters:
      json - A JSON representation of the document
      Returns:
      The document
    • createDocument

      public org.bson.Document createDocument(String key, Object value)
      Create a Document with the provided key and value.
      Parameters:
      key - The key
      value - The value
      Returns:
      The document
    • getDatabase

      public MongoDatabase getDatabase(String database)
      Get a MongoDatabase with the given name.
      Parameters:
      database - The name of the database
      Returns:
      The database
    • getDatabaseNames

      public MongoIterable<String> getDatabaseNames()
      Get all database names.
      Returns:
      An iterable list of type MongoIterable<String> containing all database names
    • getDatabases

      public MongoIterable<org.bson.Document> getDatabases()
      Get all databases.
      Returns:
      An iterable list of type MongoIterable<org.bson.Document> containing all databases
    • doesDatabaseExist

      public boolean doesDatabaseExist(String database)
      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

      public boolean createCollection(String database, String collection)
      Create a collection in the given database
      Parameters:
      database - The name of the database where the collection should be created
      collection - The name for the collection
      Returns:
      True if the collection was created, false if it already exists in the database
    • deleteCollection

      public boolean deleteCollection(String database, String collection)
      Delete a collection in the given database
      Parameters:
      database - The name of the database where the collection should be deleted
      collection - The name of the collection
      Returns:
      True if the collection was deleted, false if it did not exist in the database
    • getCollection

      public MongoCollection<org.bson.Document> getCollection(String database, String collection)
      Get a collection from a database.
      Parameters:
      database - The name of the database to fetch from
      collection - The name of the collection to get
      Returns:
      A MongoCollection<org.bson.Document> containing Document representing the collection
    • getCollectionNames

      public ListCollectionNamesIterable getCollectionNames(String database)
      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

      public ListCollectionsIterable<org.bson.Document> getCollections(String database)
      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

      public boolean doesCollectionExist(String database, String collection)
      Check if a collection exists within a database.
      Parameters:
      database - The name of the database to check
      collection - 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 created
      collection - The name for the collection
      keys - A Bson 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 from
      collection - The name of the collection to fetch from
      filter - A Bson 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 from
      collection - The name of the collection to fetch from
      filter - A Bson object representing a filter to filter documents within the collection
      projections - The project document
      sorts - Sort criteria
      Returns:
      The first document within the collection that matched the provided filter, projections, and sort criteria
    • getDocuments

      public FindIterable<org.bson.Document> getDocuments(String database, String collection)
      Get all documents within a collection.
      Parameters:
      database - The name of the database to fetch from
      collection - 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 from
      collection - The name of the collection to fetch from
      filter - A Bson 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 collection
      collection - The collection to insert into
      document - 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 collection
      collection - The collection to insert into
      documents - 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 collection
      collection - The collection containing the document
      filter - A Bson object representing a filter to filter documents within the collection
      update - 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 collection
      collection - The collection containing the document
      filter - A Bson object representing a filter to filter documents within the collection
      update - The update that that should be applied to the first matching document
      updateOptions - A UpdateOptions 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 collection
      collection - The collection containing the document
      filter - A Bson object representing a filter to filter documents within the collection
      updates - 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 collection
      collection - The collection containing the document
      filter - A Bson object representing a filter to filter documents within the collection
      updates - The updates that should be applied to the first matching document
      updateOptions - A UpdateOptions 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 collection
      collection - The collection containing the document
      filter - A Bson object representing a filter to filter documents within the collection
      update - 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 collection
      collection - The collection containing the document
      filter - A Bson object representing a filter to filter documents within the collection
      update - The update that should be applied to the first matching document
      updateOptions - A FindOneAndUpdateOptions 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 collection
      collection - The collection containing the document
      filter - A Bson object representing a filter to filter documents within the collection
      update - 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 collection
      collection - The collection containing the document
      filter - A Bson object representing a filter to filter documents within the collection
      update - The updates that should be applied to the first matching document
      updateOptions - A FindOneAndUpdateOptions 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 collection
      collection - The collection whose documents should be updated
      filter - A Bson object representing a filter to filter documents within the collection
      update - 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 collection
      collection - The collection whose documents should be updated
      filter - A Bson object representing a filter to filter documents within the collection
      update - The update that should be applied to all matching documents
      updateOptions - A UpdateOptions 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 collection
      collection - The collection whose documents should be updated
      filter - A Bson object representing a filter to filter documents within the collection
      update - 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 collection
      collection - The collection whose documents should be updated
      filter - A Bson object representing a filter to filter documents within the collection
      update - The updates that should be applied to all matching documents
      updateOptions - A UpdateOptions 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 collection
      collection - The collection that contains the document
      filter - A Bson 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 collection
      collection - The collection that contains the documents
      filter - A Bson object representing a filter to filter documents within the collection
      Returns:
      An DeleteResult representing the outcome of the operation
    • toString

      public String toString()
      Prints a representation of this MongoDatabase in string format, including the URI and MongoClient
      Specified by:
      toString in class Database
      Returns:
      A string representation of the MongoDatabase