-1

Is there any way I can use $where in aggregate.

my collection is {"id":5,score:70}, I tried {$match:{$where:"this.id>5&&this.score>70"}}, but it doesn't work, I have to use where as my api is ainterface to mong,which is receiving query from user in general text like "SQL", so is their any way I can use $where in aggregate because as document say, I cannot use it with $match, it works fine with normal find, but how to make it work with aggregate query?

Chad Johnson
  • 21,215
  • 34
  • 109
  • 207
Phalguni Mukherjee
  • 623
  • 3
  • 11
  • 29

1 Answers1

1

No, but you don't need to use $where for that:

{'$match': {'id': {'$gt': 5}, 'score': {'$gt': 70}}}

So you'd need to have the user enter their query as a JSON string like above and then JSON.parse the string on the server to turn it into an object that you could use with $match.

As a side note, the performance of $where is terrible, so basing an API on $where queries is a very bad idea.

JohnnyHK
  • 305,182
  • 66
  • 621
  • 471