Package dev.morphia
Class DatastoreImpl
- java.lang.Object
-
- dev.morphia.DatastoreImpl
-
- All Implemented Interfaces:
AdvancedDatastore
,Datastore
- Direct Known Subclasses:
BaseMorphiaSession
public class DatastoreImpl extends Object implements AdvancedDatastore
A generic (type-safe) wrapper around mongodb collectionsDeveloper note.This is an internal item. Its function and presence are subject to change without warning. Its use is highly discouraged.
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
DatastoreImpl(com.mongodb.client.MongoClient mongoClient, MapperOptions options, String dbName)
DatastoreImpl(com.mongodb.client.MongoDatabase database, com.mongodb.client.MongoClient mongoClient, Mapper mapper, QueryFactory queryFactory)
Copy constructor for a datastore
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> Aggregation<T>
aggregate(Class<T> source)
Returns a new query bound to the kind (a specificDBCollection
)Aggregation<org.bson.Document>
aggregate(String source)
Returns a new query bound to the kind (a specificDBCollection
)AggregationPipeline
createAggregation(Class source)
Returns a new query bound to the kind (a specificDBCollection
)AggregationPipeline
createAggregation(String collection, Class<?> clazz)
Returns anAggregationPipeline
bound to the given collection and class.<T> Query<T>
createQuery(Class<T> type, org.bson.Document q)
<T> com.mongodb.client.result.DeleteResult
delete(T entity)
Deletes the given entity (by @Id)<T> com.mongodb.client.result.DeleteResult
delete(T entity, DeleteOptions options)
Deletes the given entity (by @Id), with the WriteConcernvoid
enableDocumentValidation()
Process anyValidation
annotations for document validation.void
enableValidation(EntityModel model, Validation validation)
void
ensureCaps()
ensure capped collections forEntity
(s)void
ensureIndexes()
Ensures (creating if necessary) the indexes found during class mapping<T> void
ensureIndexes(Class<T> type)
Ensures (creating if necessary) the indexes found during class mapping<T> Query<T>
find(Class<T> type)
Find all instances by type<T> Query<T>
find(String collection)
Find all instances by type in a different collection than what is mapped on the class given.<T> Query<T>
find(String collection, Class<T> type)
Find all instances by type from an alternate collectioncom.mongodb.client.ClientSession
findSession(SessionConfigurable<?> configurable)
com.mongodb.client.MongoDatabase
getDatabase()
String
getLoggedQuery(FindOptions options)
Mapper
getMapper()
<T> void
insert(List<T> entities, InsertManyOptions options)
Inserts entities in to the mapped collection.<T> void
insert(T entity)
Inserts an entity in to the mapped collection.<T> void
insert(T entity, InsertOneOptions options)
Inserts an entity in to the mapped collection.<T> T
merge(T entity)
Work as if you did an update with each field in the entity doing a $set; Only at the top level of the entity.<T> T
merge(T entity, InsertOneOptions options)
Work as if you did an update with each field in the entity doing a $set; Only at the top level of the entity.<T> Query<T>
queryByExample(String collection, T ex)
Returns a new query based on the example object<T> Query<T>
queryByExample(T example)
Returns a new query based on the example object<T> void
refresh(T entity)
Refreshes an existing entity to its current state in the database.<T> List<T>
save(List<T> entities, InsertManyOptions options)
Saves the entities (Objects) and updates the @Id field<T> T
save(T entity)
Saves an entity (Object) and updates the @Id field<T> T
save(T entity, InsertOneOptions options)
Saves an entity (Object) and updates the @Id fieldMorphiaSession
startSession()
Starts a new session on the server.MorphiaSession
startSession(com.mongodb.ClientSessionOptions options)
Starts a new session on the server.<T> T
withTransaction(com.mongodb.ClientSessionOptions options, MorphiaTransaction<T> transaction)
<T> T
withTransaction(MorphiaTransaction<T> body)
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface dev.morphia.AdvancedDatastore
createRef, createRef, createUpdateOperations, insert, insert
-
Methods inherited from interface dev.morphia.Datastore
createQuery, createUpdateOperations, delete, delete, findAndDelete, findAndDelete, findAndModify, findAndModify, getSession, insert, merge, save, save, save, save, update, update
-
-
-
-
Constructor Detail
-
DatastoreImpl
protected DatastoreImpl(com.mongodb.client.MongoClient mongoClient, MapperOptions options, String dbName)
-
DatastoreImpl
public DatastoreImpl(com.mongodb.client.MongoDatabase database, com.mongodb.client.MongoClient mongoClient, Mapper mapper, QueryFactory queryFactory)
Copy constructor for a datastore- Parameters:
database
- the databasemongoClient
- the clientmapper
- the mapperqueryFactory
- the query factory- Since:
- 2.0
Developer note.This is an internal item. Its function and presence are subject to change without warning. Its use is highly discouraged.
-
-
Method Detail
-
aggregate
public Aggregation<org.bson.Document> aggregate(String source)
Description copied from interface:Datastore
Returns a new query bound to the kind (a specificDBCollection
)
-
aggregate
public <T> Aggregation<T> aggregate(Class<T> source)
Description copied from interface:Datastore
Returns a new query bound to the kind (a specificDBCollection
)
-
createAggregation
public AggregationPipeline createAggregation(Class source)
Description copied from interface:Datastore
Returns a new query bound to the kind (a specificDBCollection
)- Specified by:
createAggregation
in interfaceDatastore
- Parameters:
source
- The class to create aggregation against- Returns:
- the aggregation pipeline
-
delete
public <T> com.mongodb.client.result.DeleteResult delete(T entity)
Description copied from interface:Datastore
Deletes the given entity (by @Id)
-
delete
public <T> com.mongodb.client.result.DeleteResult delete(T entity, DeleteOptions options)
Deletes the given entity (by @Id), with the WriteConcern
-
enableDocumentValidation
public void enableDocumentValidation()
Description copied from interface:Datastore
Process anyValidation
annotations for document validation.- Specified by:
enableDocumentValidation
in interfaceDatastore
-
ensureCaps
public void ensureCaps()
Description copied from interface:Datastore
ensure capped collections forEntity
(s)- Specified by:
ensureCaps
in interfaceDatastore
-
ensureIndexes
public void ensureIndexes()
Description copied from interface:Datastore
Ensures (creating if necessary) the indexes found during class mapping- Specified by:
ensureIndexes
in interfaceDatastore
- See Also:
Indexes
,Indexed
,Text
-
createAggregation
public AggregationPipeline createAggregation(String collection, Class<?> clazz)
Description copied from interface:AdvancedDatastore
Returns anAggregationPipeline
bound to the given collection and class.- Specified by:
createAggregation
in interfaceAdvancedDatastore
- Parameters:
collection
- the collection to queryclazz
- The class to create aggregation against- Returns:
- the aggregation pipeline
-
createQuery
public <T> Query<T> createQuery(Class<T> type, org.bson.Document q)
- Specified by:
createQuery
in interfaceAdvancedDatastore
- Type Parameters:
T
- The type of the entity- Parameters:
type
- the class of objects to be returnedq
- the query which will be passed to aQueryFactory
- Returns:
- Query for the specified class type
-
find
public <T> Query<T> find(Class<T> type)
Description copied from interface:Datastore
Find all instances by type
-
find
public <T> Query<T> find(String collection, Class<T> type)
Description copied from interface:Datastore
Find all instances by type from an alternate collection
-
findSession
@Nullable public com.mongodb.client.ClientSession findSession(SessionConfigurable<?> configurable)
- Specified by:
findSession
in interfaceDatastore
- Parameters:
configurable
- the configurable- Returns:
- any session found first on the configurable then on this
-
queryByExample
public <T> Query<T> queryByExample(String collection, T ex)
Description copied from interface:AdvancedDatastore
Returns a new query based on the example object- Specified by:
queryByExample
in interfaceAdvancedDatastore
- Type Parameters:
T
- the type of the entity- Parameters:
collection
- the collection to queryex
- the example entity to use when building the query- Returns:
- the query
-
getLoggedQuery
public String getLoggedQuery(FindOptions options)
- Specified by:
getLoggedQuery
in interfaceDatastore
- Parameters:
options
- the options used when requesting logging- Returns:
- the logged query
Developer note.This is an internal item. Its function and presence are subject to change without warning. Its use is highly discouraged.
-
getMapper
public Mapper getMapper()
-
ensureIndexes
public <T> void ensureIndexes(Class<T> type)
Description copied from interface:Datastore
Ensures (creating if necessary) the indexes found during class mapping- Specified by:
ensureIndexes
in interfaceDatastore
- Type Parameters:
T
- the type to index- Parameters:
type
- the class from which to get the index definitions- See Also:
Indexes
,Indexed
,Text
-
find
public <T> Query<T> find(String collection)
Description copied from interface:Datastore
Find all instances by type in a different collection than what is mapped on the class given.
-
getDatabase
public com.mongodb.client.MongoDatabase getDatabase()
- Specified by:
getDatabase
in interfaceDatastore
- Returns:
- the MongoDatabase used by this DataStore
-
insert
public <T> void insert(T entity)
Description copied from interface:Datastore
Inserts an entity in to the mapped collection.
-
insert
public <T> void insert(T entity, InsertOneOptions options)
Description copied from interface:Datastore
Inserts an entity in to the mapped collection.
-
insert
public <T> void insert(List<T> entities, InsertManyOptions options)
Description copied from interface:Datastore
Inserts entities in to the mapped collection.
-
merge
public <T> T merge(T entity)
Description copied from interface:Datastore
Work as if you did an update with each field in the entity doing a $set; Only at the top level of the entity.
-
merge
public <T> T merge(T entity, InsertOneOptions options)
Description copied from interface:Datastore
Work as if you did an update with each field in the entity doing a $set; Only at the top level of the entity.
-
queryByExample
public <T> Query<T> queryByExample(T example)
Description copied from interface:Datastore
Returns a new query based on the example object- Specified by:
queryByExample
in interfaceDatastore
- Type Parameters:
T
- the type of the entity- Parameters:
example
- the example entity to use when creating the query- Returns:
- the query
-
refresh
public <T> void refresh(T entity)
Description copied from interface:Datastore
Refreshes an existing entity to its current state in the database. Essentially, any existing mapped state is replaced by the latest persisted state while preserving the entity's reference and object identity.
-
save
public <T> List<T> save(List<T> entities, InsertManyOptions options)
Description copied from interface:Datastore
Saves the entities (Objects) and updates the @Id field
-
save
public <T> T save(T entity)
Description copied from interface:Datastore
Saves an entity (Object) and updates the @Id field
-
save
public <T> T save(T entity, InsertOneOptions options)
Description copied from interface:Datastore
Saves an entity (Object) and updates the @Id field
-
startSession
public MorphiaSession startSession()
Description copied from interface:Datastore
Starts a new session on the server.- Specified by:
startSession
in interfaceDatastore
- Returns:
- the new session reference
-
startSession
public MorphiaSession startSession(com.mongodb.ClientSessionOptions options)
Description copied from interface:Datastore
Starts a new session on the server.- Specified by:
startSession
in interfaceDatastore
- Parameters:
options
- the options to apply- Returns:
- the new session reference
-
withTransaction
public <T> T withTransaction(MorphiaTransaction<T> body)
- Specified by:
withTransaction
in interfaceDatastore
- Type Parameters:
T
- the return type- Parameters:
body
- the transaction wrapper- Returns:
- the return value
-
withTransaction
public <T> T withTransaction(com.mongodb.ClientSessionOptions options, MorphiaTransaction<T> transaction)
- Specified by:
withTransaction
in interfaceDatastore
- Type Parameters:
T
- the return type- Parameters:
options
- the session options to applytransaction
- the transaction wrapper- Returns:
- the return value
-
enableValidation
public void enableValidation(EntityModel model, Validation validation)
- Parameters:
model
- internalvalidation
- internal
Developer note.This is an internal item. Its function and presence are subject to change without warning. Its use is highly discouraged.
-
-