4.9 KiB
Requirements Document
Introduction
This feature addresses a MongoDB compatibility issue where the current implementation uses the $unionWith aggregation operator, which is only available in MongoDB 4.4+. The system needs to support MongoDB 3.x environments by replacing the $unionWith approach with a compatible solution that queries collections separately and performs deduplication in application code.
Glossary
- Inspection_Result_System: The secondary circuit inspection result management system
- MongoDB_Driver: The C# MongoDB driver used to interact with MongoDB
- Collection_Sharding: The practice of splitting data across multiple collections based on time periods (year-month)
- Deduplication: The process of removing duplicate inspection results based on day, inspection item ID, and status
- Aggregation_Pipeline: MongoDB's framework for data aggregation operations
Requirements
Requirement 1: Replace $unionWith with Compatible Approach
User Story: As a system administrator, I want the inspection result query to work on MongoDB 3.x, so that I don't need to upgrade the database server.
Acceptance Criteria
- WHEN the system queries inspection results across multiple collections, THE Inspection_Result_System SHALL NOT use the $unionWith operator
- WHEN querying multiple collections, THE Inspection_Result_System SHALL query each collection separately using standard MongoDB operations
- WHEN multiple collection queries complete, THE Inspection_Result_System SHALL merge the results in application memory
- THE Inspection_Result_System SHALL maintain the same deduplication logic (by Year, Month, Day, SecondaryCircuitInspectionItemId, and Status)
- THE Inspection_Result_System SHALL maintain the same sorting and pagination behavior as the current implementation
Requirement 2: Preserve Query Performance
User Story: As a system user, I want query performance to remain acceptable, so that the application remains responsive.
Acceptance Criteria
- WHEN querying inspection results, THE Inspection_Result_System SHALL complete queries within 5 seconds for typical datasets (up to 12 months of data)
- WHEN performance degrades, THE Inspection_Result_System SHALL log performance warnings with timing details
- THE Inspection_Result_System SHALL use projection to limit fields retrieved from MongoDB
- THE Inspection_Result_System SHALL apply filters at the MongoDB level before retrieving data
- WHEN sorting results, THE Inspection_Result_System SHALL perform sorting in-memory after deduplication
Requirement 3: Maintain Functional Equivalence
User Story: As a developer, I want the new implementation to produce identical results to the old implementation, so that existing functionality is not broken.
Acceptance Criteria
- WHEN deduplicating results, THE Inspection_Result_System SHALL group by the same fields (Year, Month, Day, SecondaryCircuitInspectionItemId, Status)
- WHEN multiple records exist for the same group, THE Inspection_Result_System SHALL select the first record after sorting
- WHEN paginating results, THE Inspection_Result_System SHALL return the correct page of results based on skip and limit parameters
- THE Inspection_Result_System SHALL return both the paginated results and the total count
- WHEN handling null Status values, THE Inspection_Result_System SHALL treat them consistently with the original implementation
Requirement 4: Error Handling and Logging
User Story: As a system administrator, I want clear error messages and logging, so that I can troubleshoot issues effectively.
Acceptance Criteria
- WHEN a collection query fails, THE Inspection_Result_System SHALL log the error with collection name and continue with other collections
- WHEN all collection queries fail, THE Inspection_Result_System SHALL return an appropriate error message
- THE Inspection_Result_System SHALL log the number of collections queried and the number of results from each collection
- THE Inspection_Result_System SHALL log performance metrics including query time and deduplication time
- WHEN query time exceeds thresholds, THE Inspection_Result_System SHALL log performance warnings
Requirement 5: Backward Compatibility
User Story: As a developer, I want the method signature to remain unchanged, so that calling code does not need to be modified.
Acceptance Criteria
- THE Inspection_Result_System SHALL maintain the same method signature for QueryPagedDeduplicatedResultsAcrossCollectionsAsync
- THE Inspection_Result_System SHALL accept the same parameters (collectionNames, filter, sortField, isDescending, skipCount, pageSize, cancellationToken)
- THE Inspection_Result_System SHALL return the same result type (tuple of List and long totalCount)
- THE Inspection_Result_System SHALL support cancellation via CancellationToken
- THE Inspection_Result_System SHALL handle empty collection lists by returning empty results