node-mongo-querystring icon indicating copy to clipboard operation
node-mongo-querystring copied to clipboard

Filtering string fields will not work when filter value consists of numbers

Open Halt001 opened this issue 7 years ago • 2 comments

qs.parse({"foo":">1"}) results in {"foo":{"$gt":1}} and not {"foo":{"$gt":"1"}} where the '1' is converted to a Number field automatically. If the foo field is a string field in the database than this search query will find nothing.

I haven't tested it but this will probably also be true for booleans. So you probably can't filter a text field on the word 'true'.

There should be a way to force the creation of a string filter for values that could also be a number or a boolean.

Halt001 avatar Aug 09 '17 09:08 Halt001

That is correct behaviour with the default settings. You can turn off automatic parsing of Numbers and Booleans by setting toNumber and toBoolean respectively to false like this:

const qs = new MongoQS({
  string: {
    toNumber: false,
    toBoolean: false,
  }
});

Starefossen avatar Aug 11 '17 07:08 Starefossen

Sorry fo the late reply. This doesn't help the situation where you want to combine a filter on a number field and a string field like this: qs.parse({"fooString":">1", "fooNumber":">1"}); where you expect: {"fooString":{"$gt":"1"}, "fooNumber":{"$gt":1}}

Maybe it would help if you add single quotes to the syntax to aid in discriminating string fields from number fields. Like this: qs.parse({"fooString":">'1'", "fooNumber":">1"}); This may need to be escaped on the url though.

Halt001 avatar Aug 21 '17 15:08 Halt001