Class MongoUtils<T extends MongoEntity>

java.lang.Object
org.graylog2.database.utils.MongoUtils<T>
Type Parameters:
T - Java type of the documents to interact with

public class MongoUtils<T extends MongoEntity> extends Object
Utility methods to interact with MongoDB collections of document types that extend MongoEntity. Some static methods cannot enforce that type constraint but may fail if, if used with other document types.
  • Constructor Summary

    Constructors
    Constructor
    Description
    MongoUtils(com.mongodb.client.MongoCollection<T> delegate, com.fasterxml.jackson.databind.ObjectMapper objectMapper)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Convenience method to delete a single document identified by its ID.
    boolean
    deleteById(org.bson.types.ObjectId id)
    Convenience method to delete a single document identified by its ID.
    Convenience method to look up a single document by its ID.
    getById(org.bson.types.ObjectId id)
    Convenience method to look up a single document by its ID.
    getOrCreate(T entity)
    Convenience method to atomically get or create the given entity.
    static org.bson.conversions.Bson
    Create a query constraint to match a document's ID against the given Hex string.
    static org.bson.conversions.Bson
    idEq(org.bson.types.ObjectId id)
    Create a query constraint to match a document's ID.
    static org.bson.conversions.Bson
    idsIn(Collection<org.bson.types.ObjectId> ids)
    Create a query constraint to match a document's ID against a list of IDs.
    void
    initializeLegacyMongoJackBsonObject(org.mongojack.InitializationRequiredForTransformation mongoJackBsonObject)
    Deprecated.
    This method is only meant as an interim solution.
    static org.bson.types.ObjectId
    insertedId(com.mongodb.client.result.InsertOneResult result)
    Extract the inserted id of type ObjectId from the insert result.
    static String
    insertedIdAsString(com.mongodb.client.result.InsertOneResult result)
    Extract the inserted id as a String from the insert result.
    static org.bson.conversions.Bson
    objectIdEq(String fieldName, String id)
    Create a query constraint to match a field against an ObjectId.
    static org.bson.conversions.Bson
    objectIdEq(String fieldName, org.bson.types.ObjectId objectId)
    Create a query constraint to match a field against an ObjectId.
    static <T> Stream<T>
    stream(com.mongodb.client.MongoIterable<T> mongoIterable)
    Create a stream of entries from the given MongoIterable.
    static org.bson.conversions.Bson
    Create a query constraint to match a document's ID against the given list of Hex strings.

    Methods inherited from class java.lang.Object

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

    • MongoUtils

      public MongoUtils(com.mongodb.client.MongoCollection<T> delegate, com.fasterxml.jackson.databind.ObjectMapper objectMapper)
  • Method Details

    • insertedId

      public static org.bson.types.ObjectId insertedId(@Nonnull com.mongodb.client.result.InsertOneResult result)
      Extract the inserted id of type ObjectId from the insert result.
      Parameters:
      result - Result object for inserting a document of type MongoEntity.
      Returns:
      the inserted object ID. Fails if the id was not stored as an ObjectId.
    • insertedIdAsString

      public static String insertedIdAsString(@Nonnull com.mongodb.client.result.InsertOneResult result)
      Extract the inserted id as a String from the insert result.
      Parameters:
      result - Result object for inserting a document of type MongoEntity.
      Returns:
      the inserted object ID as string. Fails if the id was not stored as an ObjectId.
    • idEq

      public static org.bson.conversions.Bson idEq(@Nonnull String id)
      Create a query constraint to match a document's ID against the given Hex string.
      Parameters:
      id - Hex string representation of an ObjectId
      Returns:
      An 'eq' filter.
    • idEq

      public static org.bson.conversions.Bson idEq(@Nonnull org.bson.types.ObjectId id)
      Create a query constraint to match a document's ID.
      Parameters:
      id - ID to match
      Returns:
      An 'eq' filter.
    • objectIdEq

      public static org.bson.conversions.Bson objectIdEq(@Nonnull String fieldName, @Nonnull String id)
      Create a query constraint to match a field against an ObjectId.
      Parameters:
      fieldName - the field to check
      id - the String value of the ObjectId
      Returns:
      the filter
    • objectIdEq

      public static org.bson.conversions.Bson objectIdEq(@Nonnull String fieldName, @Nonnull org.bson.types.ObjectId objectId)
      Create a query constraint to match a field against an ObjectId.
      Parameters:
      fieldName - the field to check
      objectId - the ObjectId
      Returns:
      the filter
    • stringIdsIn

      public static org.bson.conversions.Bson stringIdsIn(Collection<String> ids)
      Create a query constraint to match a document's ID against the given list of Hex strings.
      Parameters:
      ids - Collection of hex string representations of an ObjectId
      Returns:
      An 'in' filter.
    • idsIn

      public static org.bson.conversions.Bson idsIn(Collection<org.bson.types.ObjectId> ids)
      Create a query constraint to match a document's ID against a list of IDs.
      Parameters:
      ids - IDs to match
      Returns:
      An 'in' filter.
    • stream

      public static <T> Stream<T> stream(@Nonnull com.mongodb.client.MongoIterable<T> mongoIterable)
      Create a stream of entries from the given MongoIterable. Using this method will create a stream that properly closes the underlying MongoDB cursor when the stream is closed.

      The stream should be closed to free underlying resources.

      Type Parameters:
      T - document type of the underlying collection
      Parameters:
      mongoIterable - The iterable to create the stream from.
      Returns:
      A stream that should be used in a try-with-resources statement or closed manually to free underlying resources.
    • getById

      public Optional<T> getById(org.bson.types.ObjectId id)
      Convenience method to look up a single document by its ID.
      Parameters:
      id - the document's id.
      Returns:
      the document wrapped in an Optional if present in the DB, an empty Optional otherwise.
    • getById

      public Optional<T> getById(String id)
      Convenience method to look up a single document by its ID.
      Parameters:
      id - Hex string representation of the document's ObjectId.
      Returns:
      the document wrapped in an Optional if present in the DB, an empty Optional otherwise.
    • deleteById

      public boolean deleteById(org.bson.types.ObjectId id)
      Convenience method to delete a single document identified by its ID.
      Parameters:
      id - the document's id.
      Returns:
      true if a document was deleted, false otherwise.
    • deleteById

      public boolean deleteById(String id)
      Convenience method to delete a single document identified by its ID.
      Parameters:
      id - Hex string representation of the document's ObjectId.
      Returns:
      true if a document was deleted, false otherwise.
    • getOrCreate

      public T getOrCreate(T entity)
      Convenience method to atomically get or create the given entity. If the collection doesn't contain an entity with the entity's ID, it will be created and returned. If the entity exists, the method returns the unmodified entity from the collection.

      The entity's ID must not be null!

      Parameters:
      entity - the entity to
      Returns:
      the existing or newly created entity
      Throws:
      NullPointerException - when the entity or entity ID is null
    • initializeLegacyMongoJackBsonObject

      @Deprecated public void initializeLegacyMongoJackBsonObject(org.mongojack.InitializationRequiredForTransformation mongoJackBsonObject)
      Deprecated.
      This method is only meant as an interim solution. Rewrite your deprecated MongoJack objects so that you don't have to use it.
      Utility method to help moving away from the deprecated MongoJack Bson objects, like DBQuery.Query. These objects require initialization before they can be used as regular Bson objects with the MongoDB driver.

      The JacksonMongoCollection would usually take care of that, but because we cannot use it, and instead use a regular MongoCollection, we have to use this method.