const mixinafMorphia::Datastore
afMorphia::Datastore
(Service) - Wraps a MongoDB Collection, converting Fantom entities to / from BSON documents.
When injecting as a service, use the @Inject.type attribute to state the Entity type:
@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 MongoColl collection()- The underlying MongoDB collection this Datastore wraps. 
- connMgr
- abstract MongoConnMgr connMgr()- The backing connector manager instance. 
- count
- abstract Int count(|MongoQ? queryFn := null)- Returns the number of documents that would be returned by the given - query.
- db
- abstract MongoDb db()- Creates an - MongoDbinstance of the associated DB.
- delete
- abstract Void delete(Obj entity, Bool checked := true)- Deletes the given entity from the MongoDB. Throws an - Errif- checkedand nothing was deleted.
- deleteAll
- abstract Int deleteAll()- Deletes all entities in the Datastore. Returns the number of entities deleted. - Note this is MUCH quicker than dropping the Collection. 
- deleteById
- abstract Void deleteById(Obj id, Bool checked := true)- Deletes entity with the given Id. Throws an - Errif- checkedand nothing was deleted.
- drop
- abstract This drop(Bool force := false)- Drops the underlying MongoDB collection. - Note that deleting all documents is MUCH quicker than dropping the Collection. 
- exists
- abstract Bool exists()- Returns - trueif the underlying MongoDB collection exists.
- find
- abstract MorphiaCur find([Str:Obj?]? query := null, |MongoCmd? optsFn := null)- A general purpose - find()method whose cursor returns converted entity objects.- find(["rick":"morty"]) { it->sort = ["fieldName":1] it->hint = "_indexName_" it->skip = 50 it->limit = 100 it->projection = ["_id":1, "name":1] it->batchSize = 101 it->singleBatch = true it->collation = [...] }.toList- The given query may generated from - query().
- findAll
- abstract Obj[] findAll(Obj? sort := null, |MongoQ? queryFn := null)- Returns a list of entities that match the given - query.- Use - find()if you need to set any options, like- limitor- skip.- sortmay one of:- Str- the name an index to be used as a hint
- Str:Obj?- a ordered sort document of field names with the standard Mongo- 1and- -1values for ascending / descending
- Field- the field to use for an (ascending) sort, use- reverse()on the returned list for descending sorts
 
- findOne
- abstract Obj? findOne(Bool checked, |MongoQ queryFn)- An (optimised) method to return one document from the given - query.- Throws an - Errif no documents are found and- checkedis- true. Always throws an- Errif the query returns more than one document.
- fromBsonDoc
- abstract Obj? fromBsonDoc([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 - trueif the collection has no documents.- Convenience for - datastore.exists && datastore.size == 0.
- make
- static new make(Type entityType, MongoConnMgr connMgr, BsonConvs? bsonConvs := null, Str? dbName := null)- Create a new Datastore instance. 
- name
- abstract Str name()- The name of the associated Mongo Collection. 
- query
- abstract MongoQ query()- Returns a - MongoQthat accepts fields as keys, and converts all values to BSON.- Use the result of query with - find().
- size
- abstract Int size()- Returns the number of documents in the collection. 
- toBsonDoc
- abstract [Str:Obj?]? toBsonDoc(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 Obj update(Obj entity, Bool checked := true)- Updates (and returns) the given entity. Throws an - Errif- checkedand nothing was updated.- Will always throw - OptimisticLockErrif the entity contains a- _versionfield which does not match what's in the database. On a successful save, this will increment the- _versionfield on the entity.