From 3017981090c7b08170be805ef0fbe7695ad9183b Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Mon, 24 Apr 2023 12:57:40 +0100 Subject: [PATCH] Adjust pis query to check last element Signed-off-by: Fred Boniface --- src/services/pis.services.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/services/pis.services.js b/src/services/pis.services.js index fb6ce8e..60423b8 100644 --- a/src/services/pis.services.js +++ b/src/services/pis.services.js @@ -9,19 +9,19 @@ async function findPisByOrigDest(start,end) { const firstCrs = clean.cleanApiEndpointTxt(start.toLowerCase()) const lastCrs = clean.cleanApiEndpointTxt(end.toLowerCase()) const query = { - stops: { - $all: [ - { $elemMatch: { $eq: firstCrs } }, - { $elemMatch: { $eq: lastCrs } } - ] - }, - $expr: { - $eq: [ - { $arrayElemAt: [ "$stops", 0 ] }, - firstCrs - ] - } - } + stops: { + $all: [ + { $elemMatch: { $eq: firstCrs } }, + { $elemMatch: { $eq: lastCrs } } + ] + }, + $expr: { + $and: [ + { $eq: [{ $arrayElemAt: [ "$stops", -1 ] }, lastCrs] }, + { $eq: [{ $arrayElemAt: [ "$stops", 0 ] }, firstCrs] } + ] + } + } //const oldQuery = {$and:[{$expr:{$eq:[{$first:"$stops"},firstCrs]}},{$expr:{$eq:[{$last:"$stops"},lastCrs]}}]} const search = db.query("pis", query) return search