AletheiaFact/aletheia

Refactor ClaimReviewTaskModel Aggregation Queries with `lookup` for Mongoose `populate` Usage

Opened this issue · 0 comments

Background Information

Currently, the system relies on MongoDB aggregations with lookup operations to fetch related documents, such as ClaimReview, for a given ClaimReviewTaskModel. However, a challenge arises when trying to replace the lookup for ClaimReview using Mongoose's populate due to the data_hash field being of type string. Since populate is designed for ObjectId references, a solution involves updating the schema by saving the claimReview ID on the ClaimReviewTaskModel. This adjustment enables a seamless transition from aggregations to Mongoose's find with populate.

What

The task involves modifying the ClaimReviewTaskModel schema to include a direct reference to the claimReview document by saving its ID. This modification facilitates the use of Mongoose's populate to fetch related documents efficiently.

How

  1. Update ReviewTaskMachineContext type: Modify the ReviewTaskMachineContext type to include a field (claimReview) that stores the ID of the related ClaimReview. This allows for easy reference when using populate.
export type ReviewTaskMachineContext = {
    reviewData: ReviewTaskMachineContextReviewData;
    claimReview: { // Probably a good chance to rename this field
        usersId?: User[];
        data_hash: string;
        personality: Personality;
        claim: Claim;
        + claimReview: ClaimReview;
    };
    preloadedOptions: {
        //....
    };
};
  1. Adjust Data: Ensure that existing data in the ClaimReviewTaskModel collection is updated to include the correct claimReviewId values.

  2. Modify Find Function: Adjust the _buildPipeline function to use Mongoose's find with populate. Update the population of machine.context.claimReview.claim to utilize the newly added claimReviewId field.

_buildPipeline(value, filterUser, nameSpace, page, pageSize, order) {
        const query = getQueryMatchForMachineValue(value);

        if (filterUser === true) {
            query["machine.context.reviewData.usersId"] = Types.ObjectId(
                this.req.user._id
            );
        }

        let pipeline = this.ClaimReviewTaskModel.find(query)
            .populate({
                path: 'machine.context.reviewData.usersId',
                model: 'User',
                select: 'name',
            })
            .populate({
                path: 'machine.context.claimReview.personality',
                model: 'Personality',
                select: 'slug name _id',
            })
            .populate({
                path: 'machine.context.claimReview.claim',
                model: 'Claim',
                populate: {
                    path: 'latestRevision',
                    model: 'ClaimRevision',
                },
                match: {
                    "nameSpace": nameSpace,
                    "isDeleted": false,
                },
            })
            .sort({ _id: order === "asc" ? 1 : -1 })
            .skip(page * pageSize)
            .limit(pageSize)
    }

    if (value === "published) {
         pipeline = pipeline.populate({
                path: 'machine.context.claimReview.claimReview',
                model: 'ClaimReview',
                $match: {
                     "isDeleted: false,
                }
            })
    }
    
    return pipeline

Acceptance Criteria

  • The ClaimReviewTaskModel schema includes a field named claimReview to store the ID of the related ClaimReview.
  • Existing data in the ClaimReviewTaskModel collection is updated to include the correct claimReviewId values.
  • The _buildPipeline function is updated to use Mongoose's find with populate for fetching ClaimReviewTaskModel documents.
  • The lookup for ClaimReview using populate successfully retrieves related documents based on the modified schema.
  • The modified code is tested and performs efficiently.
  • The transition from MongoDB aggregations to Mongoose's find with populate is documented for future reference.