package org.mongodb.morphia;

import com.mongodb.DBDecoderFactory;
import com.mongodb.DBObject;
import com.mongodb.DBRef;
import com.mongodb.ReadPreference;
import com.mongodb.WriteConcern;
import com.mongodb.WriteResult;
import com.mongodb.client.MongoCollection;
import org.mongodb.morphia.aggregation.AggregationPipeline;
import org.mongodb.morphia.query.Query;
import org.mongodb.morphia.query.UpdateOperations;

This interface exposes advanced Datastore features, like interacting with DBObject and low-level options. It implements matching methods from the Datastore interface but with a specified kind (collection name), or raw types (DBObject).
Author:ScottHernandez
/** * This interface exposes advanced {@link Datastore} features, like interacting with DBObject and low-level options. It implements matching * methods from the {@code Datastore} interface but with a specified kind (collection name), or raw types (DBObject). * * @author ScottHernandez */
public interface AdvancedDatastore extends Datastore {
Returns the DBDecoderFactory used by this Datastore
See Also:
Returns:the decoder factory
/** * Returns the DBDecoderFactory used by this Datastore * * @return the decoder factory * @see DBDecoderFactory */
DBDecoderFactory getDecoderFact();
Sets the DBDecoderFactory to use in this Datastore
Params:
  • fact – the DBDecoderFactory to use
/** * Sets the DBDecoderFactory to use in this Datastore * * @param fact the DBDecoderFactory to use */
void setDecoderFact(DBDecoderFactory fact);
Returns an AggregationPipeline bound to the given collection and class.
Params:
  • collection – the collection to query
  • clazz – The class to create aggregation against
Returns:the aggregation pipeline
/** * Returns an {@link AggregationPipeline} bound to the given collection and class. * * @param collection the collection to query * @param clazz The class to create aggregation against * @return the aggregation pipeline */
AggregationPipeline createAggregation(String collection, Class<?> clazz);
Params:
  • collection – the collection to query
  • clazz – the class of objects to be returned
Type parameters:
  • <T> – The type of the entity
Returns:Query for the specified class clazz
/** * @param <T> The type of the entity * @param collection the collection to query * @param clazz the class of objects to be returned * @return Query for the specified class clazz */
<T> Query<T> createQuery(String collection, Class<T> clazz);
Params:
  • clazz – the class of objects to be returned
  • q – the query which will be passed to a QueryFactory
Type parameters:
  • <T> – The type of the entity
Returns:Query for the specified class clazz
/** * @param <T> The type of the entity * @param clazz the class of objects to be returned * @param q the query which will be passed to a {@link org.mongodb.morphia.query.QueryFactory} * @return Query for the specified class clazz */
<T> Query<T> createQuery(Class<T> clazz, DBObject q);
Params:
  • collection – the collection to query
  • clazz – the class of objects to be returned
  • q – the query which will be passed to a QueryFactory
Type parameters:
  • <T> – The type of the entity
Returns:Query for the specified class clazz
/** * @param <T> The type of the entity * @param collection the collection to query * @param clazz the class of objects to be returned * @param q the query which will be passed to a {@link org.mongodb.morphia.query.QueryFactory} * @return Query for the specified class clazz */
<T> Query<T> createQuery(String collection, Class<T> clazz, DBObject q);
Creates a reference to the entity (using the current DB -can be null-, the collectionName, and id)
Params:
  • clazz – The type of the entity
  • id – The ID value of the entity
Type parameters:
  • <T> – The type of the entity
  • <V> – The type of the ID value
Returns:the DBRef for the entity
/** * Creates a reference to the entity (using the current DB -can be null-, the collectionName, and id) * * @param clazz The type of the entity * @param id The ID value of the entity * @param <T> The type of the entity * @param <V> The type of the ID value * @return the DBRef for the entity */
<T, V> DBRef createRef(Class<T> clazz, V id);
Creates a reference to the entity (using the current DB -can be null-, the collectionName, and id)
Params:
  • entity – the entity to create a DBRef for
Type parameters:
  • <T> – The type of the entity
Returns:the DBRef for the entity
/** * Creates a reference to the entity (using the current DB -can be null-, the collectionName, and id) * * @param <T> The type of the entity * @param entity the entity to create a DBRef for * @return the DBRef for the entity */
<T> DBRef createRef(T entity);
Creates an UpdateOperations instance for the given type.
Params:
  • type – The type of the entity
  • ops – The operations to perform
Type parameters:
  • <T> – The type of the entity
Returns:the UpdateOperations instance
/** * Creates an UpdateOperations instance for the given type. * * @param <T> The type of the entity * @param type The type of the entity * @param ops The operations to perform * @return the UpdateOperations instance */
<T> UpdateOperations<T> createUpdateOperations(Class<T> type, DBObject ops);
Deletes an entity of the given type T, with the given id, from the collection with the name in the kind param. Validates the id, checking it's the correct type for an ID for entities of type T. The entity type clazz is used only for validation, not for filtering, therefore if you have entities of different types in the same collection ( kind), this method will delete any entity with the given id, regardless of its type.
Params:
  • kind – the collection name
  • clazz – the Class of the entity to delete
  • id – the value of the ID
Type parameters:
  • <T> – the entity type
  • <V> – is the type of the ID, for example ObjectId
Returns:the result of this delete operation.
/** * Deletes an entity of the given type T, with the given {@code id}, from the collection with the name in the {@code kind} param. * Validates the {@code id}, checking it's the correct type for an ID for entities of type {@code T}. The entity type {@code clazz} is * used only for validation, not for filtering, therefore if you have entities of different types in the same collection ({@code * kind}), * this method will delete any entity with the given {@code id}, regardless of its type. * * @param kind the collection name * @param clazz the Class of the entity to delete * @param id the value of the ID * @param <T> the entity type * @param <V> is the type of the ID, for example ObjectId * @return the result of this delete operation. */
<T, V> WriteResult delete(String kind, Class<T> clazz, V id);
Deletes an entity of the given type T, with the given id, from the collection with the name in the kind param. Validates the id, checking it's the correct type for an ID for entities of type T. The entity type clazz is used only for validation, not for filtering, therefore if you have entities of different types in the same collection ( kind), this method will delete any entity with the given id, regardless of its type.
Params:
  • kind – the collection name
  • clazz – the Class of the entity to delete
  • id – the value of the ID
  • options – the options to use when deleting
Type parameters:
  • <T> – the entity type
  • <V> – is the type of the ID, for example ObjectId
Returns:the result of this delete operation.
Since:1.3
/** * Deletes an entity of the given type T, with the given {@code id}, from the collection with the name in the {@code kind} param. * Validates the {@code id}, checking it's the correct type for an ID for entities of type {@code T}. The entity type {@code clazz} is * used only for validation, not for filtering, therefore if you have entities of different types in the same collection ({@code * kind}), * this method will delete any entity with the given {@code id}, regardless of its type. * * @param kind the collection name * @param clazz the Class of the entity to delete * @param id the value of the ID * @param options the options to use when deleting * @param <T> the entity type * @param <V> is the type of the ID, for example ObjectId * @return the result of this delete operation. * @since 1.3 */
<T, V> WriteResult delete(String kind, Class<T> clazz, V id, DeleteOptions options);
Deletes an entity of the given type T, with the given id, from the collection with the name in the kind param. Validates the id, checking it's the correct type for an ID for entities of type T. The entity type clazz is used only for validation, not for filtering, therefore if you have entities of different types in the same collection ( kind), this method will delete any entity with the given id, regardless of its type.
Params:
  • kind – the collection name
  • clazz – the Class of the entity to delete
  • id – the value of the ID
  • wc – the WriteConcern for this operation
Type parameters:
  • <T> – the entity type
  • <V> – is the type of the ID, for example ObjectId
Returns:the result of this delete operation.
Deprecated:use Datastore.delete(Class<Object>, Object, DeleteOptions)
/** * Deletes an entity of the given type T, with the given {@code id}, from the collection with the name in the {@code kind} param. * Validates the {@code id}, checking it's the correct type for an ID for entities of type {@code T}. The entity type {@code clazz} is * used only for validation, not for filtering, therefore if you have entities of different types in the same collection ({@code * kind}), * this method will delete any entity with the given {@code id}, regardless of its type. * * @param kind the collection name * @param clazz the Class of the entity to delete * @param id the value of the ID * @param wc the WriteConcern for this operation * @param <T> the entity type * @param <V> is the type of the ID, for example ObjectId * @return the result of this delete operation. * @deprecated use {@link #delete(Class, Object, DeleteOptions)} */
@Deprecated <T, V> WriteResult delete(String kind, Class<T> clazz, V id, WriteConcern wc);
Ensures (creating if necessary) the index including the field(s) + directions on the given collection name; eg fields = "field1, -field2" ({field1:1, field2:-1})
Params:
  • collection – the collection to update
  • clazz – the class from which to get the index definitions
  • fields – the fields to index
Type parameters:
  • <T> – the type to index
See Also:
Deprecated:This method uses the legacy approach for defining indexes. Switch to using annotations on entity classes or the methods in the Java driver itself.
/** * Ensures (creating if necessary) the index including the field(s) + directions on the given collection name; eg fields = "field1, * -field2" ({field1:1, field2:-1}) * * @param collection the collection to update * @param clazz the class from which to get the index definitions * @param fields the fields to index * @param <T> the type to index * @deprecated This method uses the legacy approach for defining indexes. Switch to using annotations on entity classes or the * methods in the Java driver itself. * @see MongoCollection#createIndex(org.bson.conversions.Bson, com.mongodb.client.model.IndexOptions) * @see #ensureIndexes(String, Class) */
@Deprecated <T> void ensureIndex(String collection, Class<T> clazz, String fields);
Ensures (creating if necessary) the index including the field(s) + directions on the given collection name; eg fields = "field1, -field2" ({field1:1, field2:-1})
Params:
  • collection – the collection to update
  • clazz – the class from which to get the index definitions
  • name – the name of the index to create
  • fields – the fields to index
  • unique – true if the index should enforce uniqueness on the fields indexed
  • dropDupsOnCreate – This value is no longer supported. Tells the unique index to drop duplicates silently when creating; only the first will be kept
Type parameters:
  • <T> – the type to index
See Also:
Deprecated:This method uses the legacy approach for defining indexes. Switch to using annotations on entity classes or the methods in the Java driver itself.
/** * Ensures (creating if necessary) the index including the field(s) + directions on the given collection name; eg fields = "field1, * -field2" ({field1:1, field2:-1}) * * @param collection the collection to update * @param clazz the class from which to get the index definitions * @param name the name of the index to create * @param fields the fields to index * @param unique true if the index should enforce uniqueness on the fields indexed * @param dropDupsOnCreate This value is no longer supported. Tells the unique index to drop duplicates silently when creating; * only the first will be kept * @param <T> the type to index * @deprecated This method uses the legacy approach for defining indexes. Switch to using annotations on entity classes or the * methods in the Java driver itself. * @see MongoCollection#createIndex(org.bson.conversions.Bson, com.mongodb.client.model.IndexOptions) * @see #ensureIndexes(String, Class) */
@Deprecated <T> void ensureIndex(String collection, Class<T> clazz, String name, String fields, boolean unique, boolean dropDupsOnCreate);
Ensures (creating if necessary) the indexes found during class mapping (using @Indexed, @Indexes) on the given collection name.
Params:
  • collection – the collection to update
  • clazz – the class from which to get the index definitions
Type parameters:
  • <T> – the type to index
/** * Ensures (creating if necessary) the indexes found during class mapping (using {@code @Indexed, @Indexes)} on the given collection * name. * * @param collection the collection to update * @param clazz the class from which to get the index definitions * @param <T> the type to index */
<T> void ensureIndexes(String collection, Class<T> clazz);
Ensures (creating if necessary) the indexes found during class mapping (using @Indexed, @Indexes) on the given collection name, possibly in the background
Params:
  • collection – the collection to update
  • clazz – the class from which to get the index definitions
  • background – if true, the index will be built in the background. If false, the method will block until the index is created.
Type parameters:
  • <T> – the type to index
/** * Ensures (creating if necessary) the indexes found during class mapping (using {@code @Indexed, @Indexes)} on the given collection * name, possibly in the background * * @param collection the collection to update * @param clazz the class from which to get the index definitions * @param background if true, the index will be built in the background. If false, the method will block until the index is created. * @param <T> the type to index */
<T> void ensureIndexes(String collection, Class<T> clazz, boolean background);
Checks that an entity exists for the given key or entity
Params:
  • keyOrEntity – the value to check for
  • readPreference – Uses the supplied ReadPreference for the check. If readPreference is null the preference is taken from the annotation or uses the default preference.
See Also:
Returns:the key if the entity exists
/** * Checks that an entity exists for the given key or entity * * @param keyOrEntity the value to check for * @param readPreference Uses the supplied ReadPreference for the check. If readPreference is null the preference is taken from the * annotation or uses the default preference. * @return the key if the entity exists * @see #exists(Object) */
Key<?> exists(Object keyOrEntity, ReadPreference readPreference);
Find all instances by type in a different collection than what is mapped on the class given.
Params:
  • collection – the collection to query against
  • clazz – the class to use for mapping the results
Type parameters:
  • <T> – the type to query
Returns:the query
/** * Find all instances by type in a different collection than what is mapped on the class given. * * @param collection the collection to query against * @param clazz the class to use for mapping the results * @param <T> the type to query * @return the query */
<T> Query<T> find(String collection, Class<T> clazz);
Find all instances by type in a different collection than what is mapped on the class given skipping some documents and returning a fixed number of the remaining.
Params:
  • collection – the collection to query against
  • clazz – the class to use for mapping the results
  • property – the document property to query against
  • value – the value to check for
  • offset – the number of results to skip
  • size – the maximum number of results to return
Type parameters:
  • <T> – the type to query
  • <V> – the type to filter value
Returns:the query
/** * Find all instances by type in a different collection than what is mapped on the class given skipping some documents and returning a * fixed number of the remaining. * * @param collection the collection to query against * @param clazz the class to use for mapping the results * @param property the document property to query against * @param value the value to check for * @param offset the number of results to skip * @param size the maximum number of results to return * @param <T> the type to query * @param <V> the type to filter value * @return the query */
<T, V> Query<T> find(String collection, Class<T> clazz, String property, V value, int offset, int size);
Find the given entity (by collectionName/id);
Params:
  • clazz – the class to use for mapping
  • ref – the DBRef to use when querying
Type parameters:
  • <T> – the type to fetch
Returns:the entity referenced in the DBRef. May be null.
/** * Find the given entity (by collectionName/id); * * @param clazz the class to use for mapping * @param ref the DBRef to use when querying * @param <T> the type to fetch * @return the entity referenced in the DBRef. May be null. */
<T> T get(Class<T> clazz, DBRef ref);
Finds an entity in the named collection whose id matches the value given.
Params:
  • collection – the collection to query
  • clazz – the class to use for mapping
  • id – the ID to query
Type parameters:
  • <T> – the type to fetch
  • <V> – the type of the ID
Returns:the entity with the id. May be null.
/** * Finds an entity in the named collection whose id matches the value given. * * @param collection the collection to query * @param clazz the class to use for mapping * @param id the ID to query * @param <T> the type to fetch * @param <V> the type of the ID * @return the entity with the id. May be null. */
<T, V> T get(String collection, Class<T> clazz, V id);
Gets the count this collection
Params:
  • collection – the collection to count
Returns:the collection size
/** * Gets the count this collection * * @param collection the collection to count * @return the collection size */
long getCount(String collection);
Inserts an entity in to the mapped collection.
Params:
  • entity – the entity to insert
Type parameters:
  • <T> – the type of the entity
Returns:the new key of the inserted entity
/** * Inserts an entity in to the mapped collection. * * @param entity the entity to insert * @param <T> the type of the entity * @return the new key of the inserted entity */
<T> Key<T> insert(T entity);
Inserts an entity in to the mapped collection.
Params:
  • entity – the entity to insert
  • wc – the WriteConcern to use when inserting
Type parameters:
  • <T> – the type of the entity
See Also:
Returns:the new key of the inserted entity
Deprecated:use insert(Object, InsertOptions)
/** * Inserts an entity in to the mapped collection. * * @param entity the entity to insert * @param wc the WriteConcern to use when inserting * @param <T> the type of the entity * @return the new key of the inserted entity * @see WriteConcern * @deprecated use {@link #insert(Object, InsertOptions)} */
@Deprecated <T> Key<T> insert(T entity, WriteConcern wc);
Inserts an entity in to the mapped collection.
Params:
  • entity – the entity to insert
  • options – the options to apply to the insert operation
Type parameters:
  • <T> – the type of the entity
Returns:the new key of the inserted entity
Since:1.3
/** * Inserts an entity in to the mapped collection. * * @param entity the entity to insert * @param options the options to apply to the insert operation * @param <T> the type of the entity * @return the new key of the inserted entity * @since 1.3 */
<T> Key<T> insert(T entity, InsertOptions options);
Inserts an entity in to the named collection.
Params:
  • collection – the collection to update
  • entity – the entity to insert
Type parameters:
  • <T> – the type of the entity
Returns:the new key of the inserted entity
/** * Inserts an entity in to the named collection. * * @param collection the collection to update * @param entity the entity to insert * @param <T> the type of the entity * @return the new key of the inserted entity */
<T> Key<T> insert(String collection, T entity);
Inserts an entity in to the named collection.
Params:
  • collection – the collection to update
  • entity – the entity to insert
  • options – the options to apply to the insert operation
Type parameters:
  • <T> – the type of the entity
Returns:the new key of the inserted entity
Since:1.3
/** * Inserts an entity in to the named collection. * * @param collection the collection to update * @param entity the entity to insert * @param options the options to apply to the insert operation * @param <T> the type of the entity * @return the new key of the inserted entity * @since 1.3 */
<T> Key<T> insert(String collection, T entity, InsertOptions options);
Inserts entities in to the mapped collection.
Params:
  • entities – the entities to insert
Type parameters:
  • <T> – the type of the entity
Returns:the new keys of the inserted entities
Deprecated:use insert(Iterable<Object>) instead
/** * Inserts entities in to the mapped collection. * * @param entities the entities to insert * @param <T> the type of the entity * @return the new keys of the inserted entities * @deprecated use {@link #insert(Iterable)} instead */
@Deprecated <T> Iterable<Key<T>> insert(T... entities);
Inserts entities in to the mapped collection.
Params:
  • entities – the entities to insert
Type parameters:
  • <T> – the type of the entities
Returns:the new keys of the inserted entities
/** * Inserts entities in to the mapped collection. * * @param entities the entities to insert * @param <T> the type of the entities * @return the new keys of the inserted entities */
<T> Iterable<Key<T>> insert(Iterable<T> entities);
Inserts entities in to the mapped collection.
Params:
  • entities – the entities to insert
  • wc – the WriteConcern to use when inserting
Type parameters:
  • <T> – the type of the entity
Returns:the new keys of the inserted entities
Deprecated:use insert(Iterable<Object>, InsertOptions)
/** * Inserts entities in to the mapped collection. * * @param entities the entities to insert * @param wc the WriteConcern to use when inserting * @param <T> the type of the entity * @return the new keys of the inserted entities * @deprecated use {@link #insert(Iterable, InsertOptions)} */
@Deprecated <T> Iterable<Key<T>> insert(Iterable<T> entities, WriteConcern wc);
Inserts entities in to the mapped collection.
Params:
  • entities – the entities to insert
  • options – the options to apply to the insert operation
Type parameters:
  • <T> – the type of the entity
Returns:the new keys of the inserted entities
Since:1.3
/** * Inserts entities in to the mapped collection. * * @param entities the entities to insert * @param options the options to apply to the insert operation * @param <T> the type of the entity * @return the new keys of the inserted entities * @since 1.3 */
<T> Iterable<Key<T>> insert(Iterable<T> entities, InsertOptions options);
Inserts an entity in to the named collection.
Params:
  • collection – the collection to update
  • entities – the entities to insert
Type parameters:
  • <T> – the type of the entity
See Also:
Returns:the new keys of the inserted entities
/** * Inserts an entity in to the named collection. * * @param collection the collection to update * @param entities the entities to insert * @param <T> the type of the entity * @return the new keys of the inserted entities * @see WriteConcern */
<T> Iterable<Key<T>> insert(String collection, Iterable<T> entities);
Inserts an entity in to the named collection.
Params:
  • collection – the collection to update
  • entities – the entities to insert
  • wc – the WriteConcern to use when inserting
Type parameters:
  • <T> – the type of the entity
See Also:
Returns:the new keys of the inserted entities
Deprecated:use insert(String, Iterable<Object>, InsertOptions) instead
/** * Inserts an entity in to the named collection. * * @param collection the collection to update * @param entities the entities to insert * @param wc the WriteConcern to use when inserting * @param <T> the type of the entity * @return the new keys of the inserted entities * @see WriteConcern * @deprecated use {@link #insert(String, Iterable, InsertOptions)} instead */
@Deprecated <T> Iterable<Key<T>> insert(String collection, Iterable<T> entities, WriteConcern wc);
Inserts entities in to the named collection.
Params:
  • collection – the collection to update
  • entities – the entities to insert
  • options – the options to apply to the insert operation
Type parameters:
  • <T> – the type of the entity
Returns:the new keys of the inserted entities
Since:1.3
/** * Inserts entities in to the named collection. * * @param collection the collection to update * @param entities the entities to insert * @param options the options to apply to the insert operation * @param <T> the type of the entity * @return the new keys of the inserted entities * @since 1.3 */
<T> Iterable<Key<T>> insert(String collection, Iterable<T> entities, InsertOptions options);
Returns a new query based on the example object
Params:
  • collection – the collection to query
  • example – the example entity to use when building the query
Type parameters:
  • <T> – the type of the entity
Returns:the query
/** * Returns a new query based on the example object * * @param collection the collection to query * @param example the example entity to use when building the query * @param <T> the type of the entity * @return the query */
<T> Query<T> queryByExample(String collection, T example);
Saves an entity in to the named collection.
Params:
  • collection – the collection to update
  • entity – the entity to save
Type parameters:
  • <T> – the type of the entity
Returns:the new key of the inserted entity
/** * Saves an entity in to the named collection. * * @param collection the collection to update * @param entity the entity to save * @param <T> the type of the entity * @return the new key of the inserted entity */
<T> Key<T> save(String collection, T entity);
Saves an entity in to the named collection.
Params:
  • collection – the collection to update
  • entity – the entity to save
  • wc – the WriteConcern to use when inserting
Type parameters:
  • <T> – the type of the entity
Returns:the new key of the inserted entity
Deprecated:use save(String, Object, InsertOptions) instead
/** * Saves an entity in to the named collection. * * @param collection the collection to update * @param entity the entity to save * @param wc the WriteConcern to use when inserting * @param <T> the type of the entity * @return the new key of the inserted entity * @deprecated use {@link #save(String, Object, InsertOptions)} instead */
@Deprecated <T> Key<T> save(String collection, T entity, WriteConcern wc);
Saves an entity in to the named collection.
Params:
  • collection – the collection to update
  • entity – the entity to save
  • options – the options to apply to the save operation
Type parameters:
  • <T> – the type of the entity
Returns:the new key of the inserted entity
/** * Saves an entity in to the named collection. * * @param collection the collection to update * @param entity the entity to save * @param options the options to apply to the save operation * @param <T> the type of the entity * @return the new key of the inserted entity */
<T> Key<T> save(String collection, T entity, InsertOptions options); }