Abstract persistence component that stores data in MongoDB using the official MongoDB driver.
The MongoDbPersistence class allows you to create persistence components that store data in MongoDBs using the official MongoDB driver.
Creates a new instance of the persistence component.
_dependencyResolver
The dependency resolver.
protected DependencyResolver _dependencyResolver = new DependencyResolver(_defaultConfig);
_logger
The logger.
protected CompositeLogger _logger = new CompositeLogger;
_connection
The MongoDB connection component.
protected MongoDBConnection _connection;
_collectionName
The MongoDB colleciton name.
protected String _collectionName;
_collection
The MongoDb collection object.
protected MongoCollection _collection;
_client
The MongoDB connection pool object.
protected MongoClient _client;
_databaseName
The MongoDB database name.
protected _databaseName: string
_db
The MongoDb database object.
protected MongoDatabase _db;
_maxPageSize
The maximum number of records to return from the database per request.
protected long _maxPageSize = 100;
</span
Instance methods
clear
Clears a component’s state.
public void clear(IContext context) throws ApplicationException
- context: IContext - a context to trace execution through a call chain.
clearSchema
Clears all auto-created objects
protected void clearSchema()
close
Closes the component and frees used resources.
public void close(IContext context) throws ApplicationException
- context: IContext - a context to trace execution through a call chain.
Closes the component and frees used resources.
public void configure(ConfigParams config) throws ConfigException
convertFromPublic
Converts an object value from public to internal format.
protected Document convertFromPublic(Object value)
- value: Object - object in public format to convert.
- returns: Document - converted object in internal format.
convertToPublic
Converts and object value from internal to public format.
protected T convertToPublic(Document value)
- value: Document - object in internal format to convert.
- returns: T - converted object in public format.
create
Creates a data item.
public T create(IContext context, T item)
- context: IContext - (optional) a context to trace execution through a call chain.
- item: T - item to be created.
- returns: T - created item
defineSchema
Defines the database schema
protected void defineSchema()
deleteByFilter
This method shall be called by a public deleteByFilter method from the child class that
receives FilterParams and converts them into a filter function.
public void deleteByFilter(IContext context, Bson filter)
- context: IContext - (optional) a context to trace execution through a call chain.
- filter: Bson - (optional) filter function used to filter items.
ensureIndex
Adds index definition to create it on opening.
protected void ensureIndex(Bson keys, IndexOptions options)
- keys: Bson - index keys (fields)
- options: IndexOptions - index options
getCountByFilter
Gets a number of data items retrieved by a given filter.
This method shall be called by a public getCountByFilter method from the child class that
receives FilterParams and converts them into a filter function.
protected Long getCountByFilter(IContext context, Bson filter)
- context: IContext - (optional) a context to trace execution through a call chain.
- filter: Bson - (optional) filter JSON object
- returns: Long - number of filtered items.
getListByFilter
Gets a list of data items retrieved by a given filter and sorted according to sort parameters.
This method shall be called by a public getListByFilter method from the child class that
receives FilterParams and converts them into a filter function.
protected List getListByFilter(IContext context, Bson filter, Bson sort, Bson select)
- context: IContext - (optional) a context to trace execution through a call chain.
- filter: Bson - (optional) filter function used to filter items
- sort: Bson - (optional) sorting parameters
- select: Bson - (optional) projection parameters (not used yet)
- returns: List - data list of results by filter.
getOneRandom
Gets a random item from items that match to a given filter.
This method shall be called by a public getOneRandom method from the child class
that receives FilterParams and converts them into a filter function.
protected T getOneRandom(IContext context, Bson filter)
- context: IContext - (optional) a context to trace execution through a call chain.
- filter: Bson - fileter JSON object.
- returns: T - random item.
getPageByFilter
Gets a page of data items retrieved by a given filter and sorted according to sort parameters.
This method shall be called by a public getPageByFilter method from the child class that
receives FilterParams and converts them into a filter function.
protected DataPage getPageByFilter(IContext context, Bson filter, PagingParams paging, Bson sort, Bson select)
- context: IContext - (optional) a context to trace execution through a call chain.
- filter: Bson - (optional) filter JSON object
- paging: PagingParams - (optional) paging parameters
- sort: Bson - (optional) sorting JSON object
- select: Bson - (optional) projection JSON object
- returns: DataPage - data page obtained by filtering
Examples
{@code
class MyMongoDbPersistence extends MongoDbPersistence<MyData> {
public MyMongoDbPersistence() {
super("mydata", MyData.class);
}
public MyData getByName(IContext context, String name) {
Bson filter = Filters.eq("name", name);
MyData item = _collection.find(filter).first();
return item;
}
public MyData set(IContext context, MyData item) {
Bson filter = Filters.eq("name", item.getName());
FindOneAndReplaceOptions options = new FindOneAndReplaceOptions();
options.returnDocument(ReturnDocument.AFTER);
options.upsert(true);
MyData result = _collection.findOneAndReplace(filter, item, options);
return result;
}
}
MyMongoDbPersistence persistence = new MyMongoDbPersistence();
persistence.configure(ConfigParams.fromTuples(
"host", "localhost",
"port", 27017
));
persitence.open("123");
MyData mydata = new MyData("ABC");
persistence.set("123", mydata);
persistence.getByName("123", "ABC");
System.out.println(item); // Result: { name: "ABC" }
}