The query on the API is structured like this:
../myapi/products?_q=genre,retail_price&_a=g,p&p.gt=10&p.lt=20&g.eq=POP
There are two arrays: _q which lists the query parameters and _a listing the corresponding aliases. So p -> retail_price and g -> genres
I can parse this into:
{$and : [ genre: { '$eq': 'POP' }, retail_price: { '$gt': '10' }, retail_price: { '$lt': '20' } ]}
Almost happy. But there are a couple of problems with this approach: 1. the '$eq' etc instead of $eq etc 2. the numeric value is now a string '10'
I consider (2) to be a nasty one. Since the server cannot know the type (maybe it should be '10' instead of 10).
So, I want to try another approach. And that is parsing it all into a queryString and then convert that with JSON.parse()
First I put up some query string and try it in the shell:
db.products.find({$and: [{genre: { $eq: 'ROC'}}, {retail_price: {$gt:7}}, {retail_price: {$lt:10}}]})
Works like a charm.
Then I tried this:
var queryStr = "{$and: [{genre: { $eq: 'ROC'}}, {retail_price: {$gt:7}}, {retail_price: {$lt:10}}]}";
and: (Product is a mongoose model)
Product.find(JSON.parse(queryStr), function(err, product) {
if (err)
res.send(err);
res.json(product);
});
To my surprise it did not work at all.
Also doing
console.log(JSON.stringify(JSON.parse(queryStr)));
Does not write output to the console.
What is happening here?