export function getPartialEndTiplocMatchPipeline(query: string[]) { return [ { $match: { tiplocs: { $all: query, }, }, }, { $addFields: { reversedTiplocs: { $reverseArray: "$tiplocs", }, query: { $literal: query, }, }, }, { $addFields: { reversedQuery: { $reverseArray: "$query", }, }, }, { $match: { $expr: { $eq: [ { $slice: [ "$reversedTiplocs", 0, { $size: "$reversedQuery", }, ], }, "$reversedQuery", ], }, }, }, { $addFields: { skipStops: { $subtract: [ { $size: "$tiplocs", }, { $size: "$reversedQuery", }, ], }, }, }, { $sort: { skipStops: 1, }, }, { $limit: 1, }, { $project: { code: 1, skipStops: 1, toc: 1, _id: 0, }, }, ]; } export function getFullTiplocMatchPipeline(query: string[]) { return [ { $match: { tiplocs: query, }, }, { $limit: 1, }, { $addFields: { skipStops: 0, }, }, { $project: { code: 1, toc: 1, skipStops: 1, _id: 0, }, }, ]; }