Skip to content

Commit

Permalink
feat(aggregation): allow to filter/search in sub collections
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbontor committed Feb 26, 2023
1 parent 3f691d9 commit 0375d6d
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions libs/generateAgregation.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@ const { ValidateAgregation } = require('./validation');
* @param {Object} query
* @param {string} query.collectionName
* @param {string} query.uniqueId
* @param {string} query.subSearch
* @param {Array<string>} query.fieldToSearch
* @returns
*/
const pipeline = (query) => {
const { collectionName, uniqueId, projection } = query;
const { collectionName, uniqueId, subSearch, fieldToSearch } = query;

/**
* TODO
* - custom on `pipeline` (join stage), it doesnt always have to do _query_ `$match` instead using `foreignField`
*/
return [
const queryLookup = [
{
$lookup: {
from: collectionName,
Expand All @@ -30,6 +28,24 @@ const pipeline = (query) => {
}
}
];
if (subSearch && fieldToSearch && fieldToSearch.length > 0) {
const newearch = fieldToSearch.map((el) => {
const key = `${uniqueId}.${el}`;
return {
[key]: {
$regex: subSearch,
$options: 'i'
}
};
});
queryLookup.push({
$match: {
$or: newearch
}
});
}

return queryLookup;
};

/**
Expand Down

0 comments on commit 0375d6d

Please sign in to comment.