Package org.graylog2.database.utils
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
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
ConstructorsConstructorDescriptionMongoUtils(com.mongodb.client.MongoCollection<T> delegate, com.fasterxml.jackson.databind.ObjectMapper objectMapper) -
Method Summary
Modifier and TypeMethodDescriptionbooleandeleteById(String id) Convenience method to delete a single document identified by its ID.booleandeleteById(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.BsonCreate a query constraint to match a document's ID against the given Hex string.static org.bson.conversions.BsonidEq(org.bson.types.ObjectId id) Create a query constraint to match a document's ID.static org.bson.conversions.BsonidsIn(Collection<org.bson.types.ObjectId> ids) Create a query constraint to match a document's ID against a list of IDs.voidinitializeLegacyMongoJackBsonObject(org.mongojack.InitializationRequiredForTransformation mongoJackBsonObject) Deprecated.This method is only meant as an interim solution.static org.bson.types.ObjectIdinsertedId(com.mongodb.client.result.InsertOneResult result) Extract the inserted id of typeObjectIdfrom the insert result.static StringinsertedIdAsString(com.mongodb.client.result.InsertOneResult result) Extract the inserted id as a String from the insert result.static org.bson.conversions.BsonobjectIdEq(String fieldName, String id) Create a query constraint to match a field against anObjectId.static org.bson.conversions.BsonobjectIdEq(String fieldName, org.bson.types.ObjectId objectId) Create a query constraint to match a field against anObjectId.static <T> Stream<T>stream(com.mongodb.client.MongoIterable<T> mongoIterable) Create a stream of entries from the givenMongoIterable.static org.bson.conversions.BsonstringIdsIn(Collection<String> ids) Create a query constraint to match a document's ID against the given list of Hex strings.
-
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 typeObjectIdfrom 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
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
Create a query constraint to match a document's ID against the given Hex string.- Parameters:
id- Hex string representation of anObjectId- 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
Create a query constraint to match a field against anObjectId.- Parameters:
fieldName- the field to checkid- 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 anObjectId.- Parameters:
fieldName- the field to checkobjectId- the ObjectId- Returns:
- the filter
-
stringIdsIn
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 anObjectId- Returns:
- An 'in' filter.
-
idsIn
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
Create a stream of entries from the givenMongoIterable. 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
Convenience method to look up a single document by its ID. -
getById
Convenience method to look up a single document by its ID. -
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
Convenience method to delete a single document identified by its ID.- Parameters:
id- Hex string representation of the document'sObjectId.- Returns:
- true if a document was deleted, false otherwise.
-
getOrCreate
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, likeDBQuery.Query. These objects require initialization before they can be used as regularBsonobjects with the MongoDB driver.The
JacksonMongoCollectionwould usually take care of that, but because we cannot use it, and instead use a regularMongoCollection, we have to use this method.
-