Page 1 of 1

Using $in to find a word or words issue

Posted: Sun Feb 16, 2014 3:46 pm
by Bad Addy

I am unable to find how $in works. Here is my code

precode
$.ajax({
type: 'GET',
beforeSend: function(request) {
request.setRequestHeader('X-Appery-Database-Id', 'dv_id');
},
url: 'https://api.appery.io/rest/1/db/collections/questions/',
dataType: 'json',
data: {
where: '{"question": {"$in" : ["'+searchtext+'"]}}',
sort: "-_createdAt"
},
/code/pre

This brings back data. But, say the data in the collection column has:

test
this is a test
I am testing
does testing work

and you search test, it only brings back the first 'test'! $in means contains, so why is it not bringing the rest ?

Thanks


Using $in to find a word or words issue

Posted: Sun Feb 16, 2014 3:53 pm
by Igor

You could find how to use $in in this doc: http://docs.appery.io/documentation/b...
Try to test $in with static value.


Using $in to find a word or words issue

Posted: Sun Feb 16, 2014 4:40 pm
by Bad Addy

As I am sure you noticed, I had read that already, otherwise I would not have known about '$in'. Even if i try it with a static value, how would that help in a dynamic value ?


Using $in to find a word or words issue

Posted: Sun Feb 16, 2014 4:43 pm
by Bad Addy

Even with a static 'word', it still only brings back rows with the column equal to 'test' and not containing.

Please advise ?


Using $in to find a word or words issue

Posted: Mon Feb 17, 2014 6:24 am
by Illya Stepanov

Dear Addy,

This is a simple example to understand how $in works:

Lets assume we have a table with just two collumns - "id" and "name"
and our goal is to get rows with ids: 1, 4 and 6.

for this goal we have to write next "where" structure:

pre"where": {"id": {"$in" : ["1", "4", "6"]} }/pre
-- this filter return only items with id 1, 4 and 6.

But you have a problem with searching by content and $in will not help here.
For your goals you have to use regex.

For example:
pre"where": {"name": {"$regex": "jack"} }/pre
-- for this request server returns rows with names: "jack reacher", "blackjack", "michael jackson".

Note - regexp - is a powerfull thing and allow you to make advanced search. But there is other side of the coin - values for search - need to be escaped.