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 myEntityDatastoreYou can also autobuild a Datastore instance by passing in the entity type as a ctor param:
registry.autobuild(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
MorphiaErrifcheckedand nothing was deleted.- deleteById
abstract Void deleteById(Obj id, Bool checked := true)Deletes entity with the given Id. Throws
MorphiaErrifcheckedand nothing was deleted.- drop
abstract This drop(Bool checked := true)Drops the underlying MongoDB collection.
- exists
abstract Bool exists()Returns
trueif the underlying MongoDB collection exists.- findAll
abstract Obj[] findAll(Str:Obj? query := ([Str:Obj?])[:], Obj? sort := null, Int skip := 0, Int? limit := 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
sortis a Str it should the name of an index to use as a hint.If
sortis a[Str:Obj?]map, it should be a sort document with field names as keys. Values may either be the standard Mongo1and-1for ascending / descending or the stringsASC/DESC.The
sortmap, should it contain more than 1 entry, must be ordered.- findCount
abstract Int findCount(Str:Obj? query)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, Bool checked := true)An (optomised) 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
MongoErrif no documents are found andcheckedistrue, returnsnullotherwise. Always throwsMongoErrif 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.
- name
abstract Str name()The simple name of the MongoDB collection.
- qname
abstract Str qname()The qualified name of the MongoDB collection.
- query
abstract QueryExecutor query(Query? query := null)Returns a Query object used to build Mongo queries.
- size
abstract Int size()Returns the number of documents in the collection.
- toMongo
abstract Obj? toMongo(Obj? entity)Converts the value to a Mongo object.
Convenience for calling
Converters.toMongo(...).- toMongoDoc
abstract Str:Obj? toMongoDoc(Obj entity)Converts the entity instance to a Mongo document.
Actually,
entitymaybe any Fantom object and not just an entity, for this is just a convenience for callingConverters.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
MorphiaErrifcheckedand nothing was updated.