toModel method
Converts a raw MongoDB document to a validated model.
This method processes a document through the form validation system to ensure proper data types and formatting. Optionally filters the result to include only selected fields.
Parameters:
document
- The raw MongoDB document to processselectedFields
- Optional list of field names to include in the result
Returns a map containing the validated and formatted document data.
Example:
var rawDoc = await collection.collection.findOne({'_id': objectId});
var processedDoc = await collection.toModel(
rawDoc,
selectedFields: ['name', 'email']
);
Implementation
Future<Map<String, Object?>> toModel(
Map<String, Object?> document, {
List<String>? selectedFields,
}) async {
FormResultFree formResult = await validate(document);
var res = formResult.formValues(selectedFields: selectedFields);
return res;
}