loopback icon indicating copy to clipboard operation
loopback copied to clipboard

How to query instance with "inq" and order by "inq"?

Open gendigbadig opened this issue 10 years ago • 1 comments

Hello,

I have code like this :

// inq is array of id
var inq = ["002216", "000018", "000197"];
var filterExt = { where: { id: { inq: inq } };

Radio.find(filterExt, function(err, results){
  if(err) cb(err);
  else {
      // results's index is always following "id" order
      cb(null, results);
  }
});

My expected results is the array of results are have same order like "inq". But the results order always following "id" order. Is there any way to get same results order with inq order?

Thanks. Sorry for my bad english

gendigbadig avatar May 22 '16 15:05 gendigbadig

@gendigbadig : I'm afraid that there is any straight-forward way to achieve this. You can execute a native SQL query to order your results according to your IN array:

SELECT * FROM MyTable WHERE ColID IN ('C', 'A', 'D') 
   ORDER BY 
      CASE ColID 
         WHEN 'C' THEN 0 
         WHEN 'A' THEN 1 
         ELSE 2 
      END

Hope that helps. Closing this now, should you need any assistance with this issue, please feel free to reopen and mention me, will be happy to help.

For future reference, we highly encourage you to post your questions to our other channels specific for Q&A (links here)[https://github.com/strongloop/loopback/wiki/Questions] as we are trying to limit GH space for feature requests & bug reports.

gunjpan avatar Jun 01 '16 15:06 gunjpan