org.mongodb.morphia
Interface Datastore

All Known Subinterfaces:
AdvancedDatastore
All Known Implementing Classes:
DatastoreImpl

public interface Datastore

Datastore interface to get/delete/save objects

Author:
Scott Hernandez

Method Summary
 AggregationPipeline createAggregation(Class source)
          Returns a new query bound to the kind (a specific DBCollection)
<T> Query<T>
createQuery(Class<T> kind)
          Returns a new query bound to the kind (a specific DBCollection)
<T> UpdateOperations<T>
createUpdateOperations(Class<T> kind)
          The builder for all update operations
<T,V> WriteResult
delete(Class<T> clazz, Iterable<V> ids)
          Deletes the given entities (by id)
<T,V> WriteResult
delete(Class<T> clazz, V id)
          Deletes the given entity (by id)
<T> WriteResult
delete(Query<T> q)
          Deletes the given entities based on the query
<T> WriteResult
delete(Query<T> q, WriteConcern wc)
          Deletes the given entities based on the query, with the WriteConcern
<T> WriteResult
delete(T entity)
          Deletes the given entity (by @Id)
<T> WriteResult
delete(T entity, WriteConcern wc)
          Deletes the given entity (by @Id), with the WriteConcern
 void ensureCaps()
          ensure capped DBCollections for Entity(s)
<T> void
ensureIndex(Class<T> clazz, String fields)
          Ensures (creating if necessary) the index including the field(s) + directions; eg fields = "field1, -field2" ({field1:1, field2:-1})
<T> void
ensureIndex(Class<T> clazz, String name, String fields, boolean unique, boolean dropDupsOnCreate)
          Ensures (creating if necessary) the index including the field(s) + directions; eg fields = "field1, -field2" ({field1:1, field2:-1})
 void ensureIndexes()
          Ensures (creating if necessary) the indexes found during class mapping (using @Indexed, @Indexes)
 void ensureIndexes(boolean background)
          Ensures (creating if necessary) the indexes found during class mapping (using @Indexed, @Indexes), possibly in the background
<T> void
ensureIndexes(Class<T> clazz)
          Ensures (creating if necessary) the indexes found during class mapping (using @Indexed, @Indexes)
<T> void
ensureIndexes(Class<T> clazz, boolean background)
          Ensures (creating if necessary) the indexes found during class mapping (using @Indexed, @Indexes), possibly in the background
 Key<?> exists(Object keyOrEntity)
          Does a query to check if the keyOrEntity exists in mongodb
<T> Query<T>
find(Class<T> clazz)
          Find all instances by type
<T,V> Query<T>
find(Class<T> clazz, String property, V value)
           Find all instances by collectionName, and filter property.
<T,V> Query<T>
find(Class<T> clazz, String property, V value, int offset, int size)
           Find all instances by collectionName, and filter property.
<T> T
findAndDelete(Query<T> q)
          Deletes the given entities based on the query (first item only).
<T> T
findAndModify(Query<T> q, UpdateOperations<T> ops)
          Find the first Entity from the Query, and modify it.
<T> T
findAndModify(Query<T> q, UpdateOperations<T> ops, boolean oldVersion)
          Find the first Entity from the Query, and modify it.
<T> T
findAndModify(Query<T> q, UpdateOperations<T> ops, boolean oldVersion, boolean createIfMissing)
          Find the first Entity from the Query, and modify it.
<T,V> Query<T>
get(Class<T> clazz, Iterable<V> ids)
          Find the given entities (by id); shorthand for find("_id in", ids)
<T,V> T
get(Class<T> clazz, V id)
          Find the given entity (by id); shorthand for find("_id ", id)
<T> T
get(T entity)
          Find the given entity (by collectionName/id); think of this as refresh
<T> T
getByKey(Class<T> clazz, Key<T> key)
          Find the given entity (by collectionName/id);
<T> List<T>
getByKeys(Class<T> clazz, Iterable<Key<T>> keys)
          Find the given entities (by id), verifying they are of the correct type; shorthand for find("_id in", ids)
<T> List<T>
getByKeys(Iterable<Key<T>> keys)
          Find the given entities (by id); shorthand for find("_id in", ids)
 DBCollection getCollection(Class<?> c)
           
<T> long
getCount(Class<T> clazz)
          Gets the count this kind (DBCollection)
<T> long
getCount(Query<T> query)
          Gets the count of items returned by this query; same as query.countAll()
<T> long
getCount(T entity)
          Gets the count this kind (DBCollection)
 DB getDB()
           
 WriteConcern getDefaultWriteConcern()
           
<T> Key<T>
getKey(T entity)
          Creates a (type-safe) reference to the entity; if stored this will become a DBRef
 MongoClient getMongo()
          Get the underlying MongoClient that allows connection to the MongoDB instance being used.
 QueryFactory getQueryFactory()
          Returns the current QueryFactory.
<T> MapreduceResults<T>
mapReduce(MapreduceType type, Query q, Class<T> outputType, MapReduceCommand baseCommand)
          Runs a map/reduce job at the server; this should be used with a server version 1.7.4 or higher
<T> MapreduceResults<T>
mapReduce(MapreduceType type, Query q, String map, String reduce, String finalize, Map<String,Object> scopeFields, Class<T> outputType)
          Runs a map/reduce job at the server; this should be used with a server version 1.7.4 or higher
<T> Key<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> Key<T>
merge(T entity, WriteConcern wc)
          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(T example)
          Returns a new query based on the example object
<T> Iterable<Key<T>>
save(Iterable<T> entities)
          Saves the entities (Objects) and updates the @Id field
<T> Iterable<Key<T>>
save(Iterable<T> entities, WriteConcern wc)
          Saves the entities (Objects) and updates the @Id field, with the WriteConcern
<T> Iterable<Key<T>>
save(T... entities)
          Saves the entities (Objects) and updates the @Id field
<T> Key<T>
save(T entity)
          Saves the entity (Object) and updates the @Id field
<T> Key<T>
save(T entity, WriteConcern wc)
          Saves the entity (Object) and updates the @Id field, with the WriteConcern
 void setDefaultWriteConcern(WriteConcern wc)
           
 void setQueryFactory(QueryFactory queryFactory)
          Replaces the current QueryFactory with the given value.
<T> UpdateResults
update(Key<T> key, UpdateOperations<T> ops)
          updates the entity with the operations; this is an atomic operation
<T> UpdateResults
update(Query<T> query, UpdateOperations<T> ops)
          updates all entities found with the operations; this is an atomic operation per entity
<T> UpdateResults
update(Query<T> query, UpdateOperations<T> ops, boolean createIfMissing)
          updates all entities found with the operations, if nothing is found insert the update as an entity if "createIfMissing" is true; this is an atomic operation per entity
<T> UpdateResults
update(Query<T> query, UpdateOperations<T> ops, boolean createIfMissing, WriteConcern wc)
           
<T> UpdateResults
update(T ent, UpdateOperations<T> ops)
          updates the entity with the operations; this is an atomic operation
<T> UpdateResults
updateFirst(Query<T> query, T entity, boolean createIfMissing)
          updates the first entity found with the operations, if nothing is found insert the update as an entity if "createIfMissing" is true; this is an atomic operation per entity
<T> UpdateResults
updateFirst(Query<T> query, UpdateOperations<T> ops)
          updates the first entity found with the operations; this is an atomic operation
<T> UpdateResults
updateFirst(Query<T> query, UpdateOperations<T> ops, boolean createIfMissing)
          updates the first entity found with the operations, if nothing is found insert the update as an entity if "createIfMissing" is true; this is an atomic operation per entity
<T> UpdateResults
updateFirst(Query<T> query, UpdateOperations<T> ops, boolean createIfMissing, WriteConcern wc)
           
 

Method Detail

getKey

<T> Key<T> getKey(T entity)
Creates a (type-safe) reference to the entity; if stored this will become a DBRef


exists

Key<?> exists(Object keyOrEntity)
Does a query to check if the keyOrEntity exists in mongodb


delete

<T,V> WriteResult delete(Class<T> clazz,
                         V id)
Deletes the given entity (by id)


delete

<T,V> WriteResult delete(Class<T> clazz,
                         Iterable<V> ids)
Deletes the given entities (by id)


delete

<T> WriteResult delete(Query<T> q)
Deletes the given entities based on the query


delete

<T> WriteResult delete(Query<T> q,
                       WriteConcern wc)
Deletes the given entities based on the query, with the WriteConcern


delete

<T> WriteResult delete(T entity)
Deletes the given entity (by @Id)


delete

<T> WriteResult delete(T entity,
                       WriteConcern wc)
Deletes the given entity (by @Id), with the WriteConcern


find

<T> Query<T> find(Class<T> clazz)
Find all instances by type


find

<T,V> Query<T> find(Class<T> clazz,
                    String property,
                    V value)

Find all instances by collectionName, and filter property.

This is the same as: find(clazzOrEntity).filter (property, value);


find

<T,V> Query<T> find(Class<T> clazz,
                    String property,
                    V value,
                    int offset,
                    int size)

Find all instances by collectionName, and filter property.

This is the same as: find(clazzOrEntity).filter (property, value).offset(offset).limit(size);


get

<T,V> Query<T> get(Class<T> clazz,
                   Iterable<V> ids)
Find the given entities (by id); shorthand for find("_id in", ids)


get

<T,V> T get(Class<T> clazz,
            V id)
Find the given entity (by id); shorthand for find("_id ", id)


get

<T> T get(T entity)
Find the given entity (by collectionName/id); think of this as refresh


getByKeys

<T> List<T> getByKeys(Class<T> clazz,
                      Iterable<Key<T>> keys)
Find the given entities (by id), verifying they are of the correct type; shorthand for find("_id in", ids)


getByKeys

<T> List<T> getByKeys(Iterable<Key<T>> keys)
Find the given entities (by id); shorthand for find("_id in", ids)


getByKey

<T> T getByKey(Class<T> clazz,
               Key<T> key)
Find the given entity (by collectionName/id);


getCount

<T> long getCount(T entity)
Gets the count this kind (DBCollection)


getCount

<T> long getCount(Class<T> clazz)
Gets the count this kind (DBCollection)


getCount

<T> long getCount(Query<T> query)
Gets the count of items returned by this query; same as query.countAll()


save

<T> Iterable<Key<T>> save(Iterable<T> entities)
Saves the entities (Objects) and updates the @Id field


save

<T> Iterable<Key<T>> save(Iterable<T> entities,
                          WriteConcern wc)
Saves the entities (Objects) and updates the @Id field, with the WriteConcern


save

<T> Iterable<Key<T>> save(T... entities)
Saves the entities (Objects) and updates the @Id field


save

<T> Key<T> save(T entity)
Saves the entity (Object) and updates the @Id field


save

<T> Key<T> save(T entity,
                WriteConcern wc)
Saves the entity (Object) and updates the @Id field, with the WriteConcern


merge

<T> Key<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.


merge

<T> Key<T> merge(T entity,
                 WriteConcern wc)
Work as if you did an update with each field in the entity doing a $set; Only at the top level of the entity.


update

<T> UpdateResults update(T ent,
                         UpdateOperations<T> ops)
updates the entity with the operations; this is an atomic operation


update

<T> UpdateResults update(Key<T> key,
                         UpdateOperations<T> ops)
updates the entity with the operations; this is an atomic operation


update

<T> UpdateResults update(Query<T> query,
                         UpdateOperations<T> ops)
updates all entities found with the operations; this is an atomic operation per entity


update

<T> UpdateResults update(Query<T> query,
                         UpdateOperations<T> ops,
                         boolean createIfMissing)
updates all entities found with the operations, if nothing is found insert the update as an entity if "createIfMissing" is true; this is an atomic operation per entity


update

<T> UpdateResults update(Query<T> query,
                         UpdateOperations<T> ops,
                         boolean createIfMissing,
                         WriteConcern wc)

updateFirst

<T> UpdateResults updateFirst(Query<T> query,
                              UpdateOperations<T> ops)
updates the first entity found with the operations; this is an atomic operation


updateFirst

<T> UpdateResults updateFirst(Query<T> query,
                              UpdateOperations<T> ops,
                              boolean createIfMissing)
updates the first entity found with the operations, if nothing is found insert the update as an entity if "createIfMissing" is true; this is an atomic operation per entity


updateFirst

<T> UpdateResults updateFirst(Query<T> query,
                              UpdateOperations<T> ops,
                              boolean createIfMissing,
                              WriteConcern wc)

updateFirst

<T> UpdateResults updateFirst(Query<T> query,
                              T entity,
                              boolean createIfMissing)
updates the first entity found with the operations, if nothing is found insert the update as an entity if "createIfMissing" is true; this is an atomic operation per entity


findAndDelete

<T> T findAndDelete(Query<T> q)
Deletes the given entities based on the query (first item only).

Returns:
the deleted Entity

findAndModify

<T> T findAndModify(Query<T> q,
                    UpdateOperations<T> ops)
Find the first Entity from the Query, and modify it.

Returns:
The modified Entity (the result of the update)

findAndModify

<T> T findAndModify(Query<T> q,
                    UpdateOperations<T> ops,
                    boolean oldVersion)
Find the first Entity from the Query, and modify it.

Parameters:
q - the query to find the Entity with; You are not allowed to offset/skip in the query.
oldVersion - indicated the old version of the Entity should be returned
Returns:
The Entity (the result of the update if oldVersion is false)

findAndModify

<T> T findAndModify(Query<T> q,
                    UpdateOperations<T> ops,
                    boolean oldVersion,
                    boolean createIfMissing)
Find the first Entity from the Query, and modify it.

Parameters:
q - the query to find the Entity with; You are not allowed to offset/skip in the query.
oldVersion - indicated the old version of the Entity should be returned
createIfMissing - if the query returns no results, then a new object will be created (sets upsert=true)
Returns:
The Entity (the result of the update if oldVersion is false)

mapReduce

<T> MapreduceResults<T> mapReduce(MapreduceType type,
                                  Query q,
                                  String map,
                                  String reduce,
                                  String finalize,
                                  Map<String,Object> scopeFields,
                                  Class<T> outputType)
Runs a map/reduce job at the server; this should be used with a server version 1.7.4 or higher

Type Parameters:
T - The type of resulting data
Parameters:
outputType - The type of resulting data; inline is not working yet
type - MapreduceType
q - The query (only the criteria, limit and sort will be used)
map - The map function, in javascript, as a string
reduce - The reduce function, in javascript, as a string
finalize - The finalize function, in javascript, as a string; can be null
scopeFields - Each map entry will be a global variable in all the functions; can be null
Returns:
counts and stuff

mapReduce

<T> MapreduceResults<T> mapReduce(MapreduceType type,
                                  Query q,
                                  Class<T> outputType,
                                  MapReduceCommand baseCommand)
Runs a map/reduce job at the server; this should be used with a server version 1.7.4 or higher

Type Parameters:
T - The type of resulting data
Parameters:
type - MapreduceType
q - The query (only the criteria, limit and sort will be used)
outputType - The type of resulting data; inline is not working yet
baseCommand - The base command to fill in and send to the server
Returns:
counts and stuff

createUpdateOperations

<T> UpdateOperations<T> createUpdateOperations(Class<T> kind)
The builder for all update operations


createAggregation

AggregationPipeline createAggregation(Class source)
Returns a new query bound to the kind (a specific DBCollection)


createQuery

<T> Query<T> createQuery(Class<T> kind)
Returns a new query bound to the kind (a specific DBCollection)


queryByExample

<T> Query<T> queryByExample(T example)
Returns a new query based on the example object


ensureIndex

<T> void ensureIndex(Class<T> clazz,
                     String fields)
Ensures (creating if necessary) the index including the field(s) + directions; eg fields = "field1, -field2" ({field1:1, field2:-1})


ensureIndex

<T> void ensureIndex(Class<T> clazz,
                     String name,
                     String fields,
                     boolean unique,
                     boolean dropDupsOnCreate)
Ensures (creating if necessary) the index including the field(s) + directions; eg fields = "field1, -field2" ({field1:1, field2:-1})


ensureIndexes

void ensureIndexes()
Ensures (creating if necessary) the indexes found during class mapping (using @Indexed, @Indexes)


ensureIndexes

void ensureIndexes(boolean background)
Ensures (creating if necessary) the indexes found during class mapping (using @Indexed, @Indexes), possibly in the background


ensureIndexes

<T> void ensureIndexes(Class<T> clazz)
Ensures (creating if necessary) the indexes found during class mapping (using @Indexed, @Indexes)


ensureIndexes

<T> void ensureIndexes(Class<T> clazz,
                       boolean background)
Ensures (creating if necessary) the indexes found during class mapping (using @Indexed, @Indexes), possibly in the background


ensureCaps

void ensureCaps()
ensure capped DBCollections for Entity(s)


getDB

DB getDB()

getMongo

MongoClient getMongo()
Get the underlying MongoClient that allows connection to the MongoDB instance being used.

Returns:
the MongoClient being used by this datastore.

getCollection

DBCollection getCollection(Class<?> c)

getDefaultWriteConcern

WriteConcern getDefaultWriteConcern()

setDefaultWriteConcern

void setDefaultWriteConcern(WriteConcern wc)

setQueryFactory

void setQueryFactory(QueryFactory queryFactory)
Replaces the current QueryFactory with the given value.

See Also:
QueryFactory

getQueryFactory

QueryFactory getQueryFactory()
Returns the current QueryFactory.

See Also:
QueryFactory