Please take a look at the following and edit your code accordingly.
It would also help if you could publish some sample documents from your collection and from the expected results.
It works in the shell because the expression db.getCollection(‘StocksTimeSeries’).distinct(“sym”,{“AdjClose”:{ “$lt” : 50 }}) get executed first and the the result is used as the value of the $in operator.
You might need to use the aggregation framework to accomplish what you want.