getOneRandomEx method
Gets a random item from items that match to a given filter.
This method shall be called by a public getOneRandom method from child class that receives FilterParams and converts them into a filter function.
correlationId
(optional) transaction id to trace execution through call chain.filter
(optional) a filter JSON object Return Future that receives a random item Throws error.
Implementation
Future<T?> getOneRandomEx(
String? correlationId, Map<String, dynamic> filter) async {
var query = mngquery.SelectorBuilder();
var selector = <String, dynamic>{};
selector[r'$query'] = filter;
var count = await collection?.count(query);
var pos = RandomInteger.nextInteger(0, count! - 1);
query.skip(pos >= 0 ? pos : 0);
query.limit(1);
var items = collection?.find(query.raw(selector));
try {
var item = (items != null) ? await items.first : null;
return convertToPublic(item);
} catch (ex) {
return null;
}
}