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
- Update ReviewTaskMachineContext type: Modify the
ReviewTaskMachineContext
type to include a field (claimReview
) that stores the ID of the relatedClaimReview
. This allows for easy reference when usingpopulate
.
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: {
//....
};
};
-
Adjust Data: Ensure that existing data in the
ClaimReviewTaskModel
collection is updated to include the correctclaimReviewId
values. -
Modify Find Function: Adjust the
_buildPipeline
function to use Mongoose'sfind
withpopulate
. Update the population ofmachine.context.claimReview.claim
to utilize the newly addedclaimReviewId
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 namedclaimReview
to store the ID of the relatedClaimReview
. - Existing data in the
ClaimReviewTaskModel
collection is updated to include the correctclaimReviewId
values. - The
_buildPipeline
function is updated to use Mongoose'sfind
withpopulate
for fetchingClaimReviewTaskModel
documents. - The lookup for
ClaimReview
usingpopulate
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
withpopulate
is documented for future reference.