Package dev.morphia
Class DatastoreImpl
- java.lang.Object
-
- dev.morphia.DatastoreImpl
-
- All Implemented Interfaces:
AdvancedDatastore,Datastore
- Direct Known Subclasses:
MorphiaSessionImpl
@MorphiaInternal 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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classDatastoreImpl.DatastoreOperations
-
Constructor Summary
Constructors Modifier Constructor Description protectedDatastoreImpl(DatastoreImpl datastore)Copy constructor for a datastoreprotectedDatastoreImpl(Mapper mapper, MongoClient mongoClient, String dbName)
-
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 specificMongoCollection)Aggregation<Document>aggregate(String source)Returns a new query bound to the kind (a specificMongoCollection)<T> MongoCollection<T>configureCollection(dev.morphia.internal.CollectionConfiguration options, MongoCollection<T> collection)Applies configuration options to the collectionAggregationPipelinecreateAggregation(Class source)Returns a new query bound to the kind (a specificMongoCollection)AggregationPipelinecreateAggregation(String collection, Class<?> clazz)Returns anAggregationPipelinebound to the given collection and class.<T> Query<T>createQuery(Class<T> type, Document q)<T> UpdateOperations<T>createUpdateOperations(Class<T> clazz)The builder for all update operations<T> DeleteResultdelete(T entity)Deletes the given entity (by @Id)<T> DeleteResultdelete(T entity, DeleteOptions options)Deletes the given entity (by @Id), with the WriteConcernprotected <T> TdoTransaction(MorphiaSessionImpl morphiaSession, MorphiaTransaction<T> body)voidenableDocumentValidation()Process anyValidationannotations for document validation.voidenableValidation(EntityModel model, Validation validation)voidensureCaps()ensure capped collections forEntity(s)voidensureIndexes()Ensures (creating if necessary) the indexes found during class mapping<T> voidensureIndexes(Class<T> type)<T> Query<T>find(Class<T> type)Find instances of a type<T> Query<T>find(Class<T> type, Document nativeQuery)Find instances of a type using a native query.<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 collectionCodecRegistrygetCodecRegistry()<T> MongoCollection<T>getCollection(Class<T> type)MongoDatabasegetDatabase()StringgetLoggedQuery(FindOptions options)MappergetMapper()protected MongoClientgetMongoClient()<T> voidinsert(List<T> entities, InsertManyOptions options)Inserts entities in to the mapped collection.<T> voidinsert(T entity, InsertOneOptions options)Inserts an entity in to the mapped collection.<T> Tmerge(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> Tmerge(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.DatastoreImpl.DatastoreOperationsoperations()protected DatastoreImploperations(DatastoreImpl.DatastoreOperations operations)<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> voidrefresh(T entity)Refreshes an existing entity to its current state in the database.<T> List<T>replace(List<T> entities, ReplaceOptions options)Replaces a list of documents in the database<T> Treplace(T entity, ReplaceOptions options)Replaces a document in the database<T> List<T>save(List<T> entities, InsertManyOptions options)Saves the entities (Objects) and updates the @Id field<T> Tsave(T entity, InsertOneOptions options)Saves an entity (Object) and updates the @Id fieldprotected DocumentshardCollection(EntityModel model)voidshardCollections()Shards any collections with sharding definitions.MorphiaSessionImplstartSession()Starts a new session on the server.MorphiaSessionImplstartSession(ClientSessionOptions options)Starts a new session on the server.<T> TwithTransaction(ClientSessionOptions options, MorphiaTransaction<T> transaction)<T> TwithTransaction(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, delete, delete, findAndDelete, findAndDelete, findAndModify, findAndModify, insert, insert, merge, replace, replace, save, save, save, save, save, update, update
-
-
-
-
Constructor Detail
-
DatastoreImpl
protected DatastoreImpl(Mapper mapper, MongoClient mongoClient, String dbName)
-
DatastoreImpl
protected DatastoreImpl(DatastoreImpl datastore)
Copy constructor for a datastore- Parameters:
datastore- the datastore to clone- 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
-
insert
public <T> void insert(T entity, InsertOneOptions options)Description copied from interface:DatastoreInserts an entity in to the mapped collection.
-
insert
public <T> void insert(List<T> entities, InsertManyOptions options)
Description copied from interface:DatastoreInserts entities in to the mapped collection.
-
aggregate
public Aggregation<Document> aggregate(String source)
Description copied from interface:DatastoreReturns a new query bound to the kind (a specificMongoCollection)
-
aggregate
public <T> Aggregation<T> aggregate(Class<T> source)
Description copied from interface:DatastoreReturns a new query bound to the kind (a specificMongoCollection)
-
createAggregation
public AggregationPipeline createAggregation(Class source)
Description copied from interface:DatastoreReturns a new query bound to the kind (a specificMongoCollection)- Specified by:
createAggregationin interfaceDatastore- Parameters:
source- The class to create aggregation against- Returns:
- the aggregation pipeline
-
createUpdateOperations
public <T> UpdateOperations<T> createUpdateOperations(Class<T> clazz)
Description copied from interface:DatastoreThe builder for all update operations- Specified by:
createUpdateOperationsin interfaceDatastore- Type Parameters:
T- the type to update- Parameters:
clazz- the type to update- Returns:
- the new UpdateOperations instance
-
configureCollection
@NonNull @MorphiaInternal public <T> MongoCollection<T> configureCollection(dev.morphia.internal.CollectionConfiguration options, MongoCollection<T> collection)
Applies configuration options to the collection- Type Parameters:
T- the collection type- Parameters:
options- the options to applycollection- the collection to configure- Returns:
- the configured collection
Developer note.This is an internal item. Its function and presence are subject to change without warning. Its use is highly discouraged.
-
delete
public <T> DeleteResult delete(T entity, DeleteOptions options)
Deletes the given entity (by @Id), with the WriteConcern
-
delete
public <T> DeleteResult delete(T entity)
Description copied from interface:DatastoreDeletes the given entity (by @Id)
-
enableDocumentValidation
public void enableDocumentValidation()
Description copied from interface:DatastoreProcess anyValidationannotations for document validation.- Specified by:
enableDocumentValidationin interfaceDatastore
-
ensureIndexes
public void ensureIndexes()
Description copied from interface:DatastoreEnsures (creating if necessary) the indexes found during class mapping- Specified by:
ensureIndexesin interfaceDatastore- See Also:
Indexes,Indexed,Text
-
ensureIndexes
public <T> void ensureIndexes(Class<T> type)
-
find
public <T> Query<T> find(Class<T> type)
Description copied from interface:DatastoreFind instances of a type
-
find
public <T> Query<T> find(Class<T> type, Document nativeQuery)
Description copied from interface:DatastoreFind instances of a type using a native query. This method is intended as an aid when copying queries from external sources such as the shell or Compass whose structure is already in json form.
-
find
public <T> Query<T> find(String collection, Class<T> type)
Description copied from interface:DatastoreFind all instances by type from an alternate collection
-
find
public <T> Query<T> find(String collection)
Description copied from interface:DatastoreFind all instances by type in a different collection than what is mapped on the class given.
-
getCodecRegistry
public CodecRegistry getCodecRegistry()
- Specified by:
getCodecRegistryin interfaceDatastore- Returns:
- the codec registry
-
getCollection
public <T> MongoCollection<T> getCollection(Class<T> type)
- Specified by:
getCollectionin interfaceDatastore- Type Parameters:
T- the class type- Parameters:
type- the type look up- Returns:
- the collection mapped for this class
-
getDatabase
public MongoDatabase getDatabase()
- Specified by:
getDatabasein interfaceDatastore- Returns:
- the MongoDatabase used by this DataStore
-
getLoggedQuery
public String getLoggedQuery(FindOptions options)
- Specified by:
getLoggedQueryin 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.
-
replace
public <T> T replace(T entity, ReplaceOptions options)Description copied from interface:DatastoreReplaces a document in the database
-
ensureCaps
public void ensureCaps()
Description copied from interface:Datastoreensure capped collections forEntity(s)- Specified by:
ensureCapsin interfaceDatastore
-
merge
public <T> T merge(T entity, InsertOneOptions options)Description copied from interface:DatastoreWork as if you did an update with each field in the entity doing a $set; Only at the top level of the entity.
-
getMongoClient
protected MongoClient getMongoClient()
-
getMapper
public Mapper getMapper()
-
shardCollections
public void shardCollections()
Description copied from interface:DatastoreShards any collections with sharding definitions.- Specified by:
shardCollectionsin interfaceDatastore
-
merge
public <T> T merge(T entity)
Description copied from interface:DatastoreWork as if you did an update with each field in the entity doing a $set; Only at the top level of the entity.
-
shardCollection
protected Document shardCollection(EntityModel model)
-
save
public <T> List<T> save(List<T> entities, InsertManyOptions options)
Description copied from interface:DatastoreSaves the entities (Objects) and updates the @Id field
-
queryByExample
public <T> Query<T> queryByExample(T example)
Description copied from interface:DatastoreReturns a new query based on the example object- Specified by:
queryByExamplein 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:DatastoreRefreshes 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.
-
startSession
public MorphiaSessionImpl startSession()
Description copied from interface:DatastoreStarts a new session on the server.- Specified by:
startSessionin interfaceDatastore- Returns:
- the new session reference
-
startSession
public MorphiaSessionImpl startSession(ClientSessionOptions options)
Description copied from interface:DatastoreStarts a new session on the server.- Specified by:
startSessionin interfaceDatastore- Parameters:
options- the options to apply- Returns:
- the new session reference
-
save
public <T> T save(T entity, InsertOneOptions options)Description copied from interface:DatastoreSaves an entity (Object) and updates the @Id field
-
operations
public DatastoreImpl.DatastoreOperations operations()
-
doTransaction
protected <T> T doTransaction(MorphiaSessionImpl morphiaSession, MorphiaTransaction<T> body)
-
withTransaction
public <T> T withTransaction(MorphiaTransaction<T> body)
- Specified by:
withTransactionin interfaceDatastore- Type Parameters:
T- the return type- Parameters:
body- the transaction wrapper- Returns:
- the return value
-
withTransaction
public <T> T withTransaction(ClientSessionOptions options, MorphiaTransaction<T> transaction)
- Specified by:
withTransactionin interfaceDatastore- Type Parameters:
T- the return type- Parameters:
options- the session options to applytransaction- the transaction wrapper- Returns:
- the return value
-
replace
public <T> List<T> replace(List<T> entities, ReplaceOptions options)
Description copied from interface:DatastoreReplaces a list of documents in the database
-
createAggregation
public AggregationPipeline createAggregation(String collection, Class<?> clazz)
Description copied from interface:AdvancedDatastoreReturns anAggregationPipelinebound to the given collection and class.- Specified by:
createAggregationin interfaceAdvancedDatastore- Parameters:
collection- the collection to queryclazz- The class to create aggregation against- Returns:
- the aggregation pipeline
- See Also:
Aggregation,AggregationOptions
-
createQuery
public <T> Query<T> createQuery(Class<T> type, Document q)
- Specified by:
createQueryin 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
-
queryByExample
public <T> Query<T> queryByExample(String collection, T ex)
Description copied from interface:AdvancedDatastoreReturns a new query based on the example object- Specified by:
queryByExamplein 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
-
enableValidation
@MorphiaInternal 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.
-
operations
protected DatastoreImpl operations(DatastoreImpl.DatastoreOperations operations)
-
-