const mixinafMorphia::Datastore
afMorphia::Datastore
(Service) - Wraps a MongoDB Collection, converting Fantom entities to / from Mongo documents.
When injecting as a service, use the @Inject.type
attribute to state which Entity type it is for:
@Inject { type=MyEntity# } private const Datastore myEntityDatastore
You can also autobuild a Datastore instance by passing in the entity type as a ctor param:
scope.build(Datastore#, [MyEntity#])
- collection
abstract Collection collection()
The underlying MongoDB collection this Datastore wraps.
- delete
abstract Void delete(Obj entity, Bool checked := true)
Deletes the given entity from the MongoDB. Throws
MorphiaErr
ifchecked
and nothing was deleted.- deleteById
abstract Void deleteById(Obj id, Bool checked := true)
Deletes entity with the given Id. Throws
MorphiaErr
ifchecked
and nothing was deleted.- drop
abstract This drop(Bool force := false)
Drops the underlying MongoDB collection.
- exists
abstract Bool exists()
Returns
true
if the underlying MongoDB collection exists.- findAll
abstract Obj[] findAll([Str:Obj?]? query := null, Obj? sort := null, Int skip := 0, Int? limit := null, [Str:Obj?]? projection := null)
Returns a list of entities that match the given
query
.Note: This method requires you to be familiar with Mongo query notation. If not, use the Query builder instead.
If
sort
is a Str it should the name of an index to use as a hint.If
sort
is a[Str:Obj?]
map, it should be a sort document with field names as keys. Values may either be the standard Mongo1
and-1
for ascending / descending or the stringsASC
/DESC
.The
sort
map, should it contain more than 1 entry, must be ordered.- findCount
abstract Int findCount([Str:Obj?]? query := null)
Returns the number of documents that would be returned by the given
query
.Note: This method requires you to be familiar with Mongo query notation. If not, use the Query builder instead.
- findOne
abstract Obj? findOne([Str:Obj?]? query := null, Bool checked := true)
An (optimised) method to return one document from the given
query
.Note: This method requires you to be familiar with Mongo query notation. If not, use the Query builder instead.
Throws
MongoErr
if no documents are found andchecked
istrue
, returnsnull
otherwise. Always throwsMongoErr
if the query returns more than one document.- fromMongoDoc
abstract Obj fromMongoDoc(Str:Obj? mongoDoc)
Converts the Mongo document to an entity instance.
The returned object is not guaranteed to be of any particular object, for this is just a convenience for calling
Converters.toFantom(...)
.- get
@
Operator
abstract Obj? get(Obj? id, Bool checked := true)Returns the document with the given Id. Convenience / shorthand notation for
findOne(["_id": id], checked)
- insert
abstract Obj insert(Obj entity)
Inserts the given entity. Returns the entity.
- isEmpty
abstract Bool isEmpty()
Returns
true
if the collection has no documents.Convenience for
datastore.exists && datastore.size == 0
.- name
abstract Str name()
The simple name of the MongoDB collection.
- qname
abstract Str qname()
The qualified name of the MongoDB collection. It takes the form of:
<database>.<collection>
- query
abstract QueryExecutor query(Query? query := null)
Use to execute the given query.
- size
abstract Int size()
Returns the number of documents in the collection.
- toMongoDoc
abstract Str:Obj? toMongoDoc(Obj entity)
Converts the entity instance to a Mongo document.
Convenience for calling
Converters.toMongo(...)
.- type
abstract Type type()
The Fantom entity type this Datastore associates with.
- update
abstract Void update(Obj entity, Bool? upsert := (Bool?)false, Bool checked := true)
Updates the given entity. Throws
MorphiaErr
ifchecked
and nothing was updated.