Page 1 of 2

Query a an exact string while ignoring case

Posted: Sat Oct 05, 2013 3:03 pm
by bahar.wadia

I am trying to query on a string. I want a value returned only if the entire string is matched, but I want to ignore case.

How do I do it ?

The below works well, except that it will return values even is the string contains part of the expression. In the below expression, I expect to get a record where the string matches "wrench"

{"ItemType":{"$regex":"wrench", "$options":"i"}}';

however, the below also returns the same exact result as the above.

{"ItemType":{"$regex":"renc", "$options":"i"}}';

Obviously, I am missing something. Please help.

Thanks


Query a an exact string while ignoring case

Posted: Sat Oct 05, 2013 3:44 pm
by Alena Prykhodko

Hello!

This doc should help http://docs.appery.io/documentation/b...


Query a an exact string while ignoring case

Posted: Sat Oct 05, 2013 4:12 pm
by bahar.wadia

Yes, I have read through this information many times over. I could have missed the obvious, but can you point me to the exact location on that page that answers my question. Alternatively, can you please provide me with an example.

Thanks


Query a an exact string while ignoring case

Posted: Sat Oct 05, 2013 4:53 pm
by Alena Prykhodko

Use this:
pre
{"ItemType":{"$regex":"wrench$", "$options":"i"}}'; /pre

instead of :
pre
{"ItemType":{"$regex":"wrench", "$options":"i"}}'; /pre

^ - shows string's start;
$ - shows string's end.


Query a an exact string while ignoring case

Posted: Sat Oct 05, 2013 6:20 pm
by bahar.wadia

Awesome!

That did the trick.

Also, is there any place or book you can recommend that has comprehensive coverage of REST. I hate to have to keep bugging you guys for these basics. Not that I mind the helpful service.


Query a an exact string while ignoring case

Posted: Tue Aug 05, 2014 9:56 pm
by R2R

I'm having a similar issue with adding a line of script in the Server Code tab. I need the search for a username in the database ignoring case for ANY of the letters in the username. The line of code looks like this so far:

"parameters": {
"where": '{"username": "' + encodeURIComponent(username) + '"}'
}

I'm not sure where to ADD this:
{"username": {"$regex": "username$", "$options":"i"}}';

Can you adjust this code exactly how it should be and place it exactly where it should be within the "parameters" statement?

Thank you!


Query a an exact string while ignoring case

Posted: Wed Aug 06, 2014 3:56 am
by Yurii Orishchuk

Hi R2R,

You need to set this code in code mapping for user query service.

See details: http://prntscr.com/4a0g3w/direct

Here is little modified code to better understand it:

pre

//You can modify this string in way you need.
var userNameString = "nick";

var whereObject = {"username":{"$regex":userNameString, "$options":"i"}};

return JSON.stringify(whereObject);

/pre

Regards.


Query a an exact string while ignoring case

Posted: Wed Aug 06, 2014 4:06 pm
by R2R

Yurii, thank you for responding, but I don't think I want to modify my code per your suggestion. I followed the tutorial on appery.io website to create an app with user registration and password reset functionality and everything is working as it should using the sendEmail and resetPassword server scripts. However, I need to be able to search for an existing user in the Users database disregarding letter case. I need to combine #1 and #2 below into one statement with correct syntax. #1 already works perfectly, but I need to add #2 to it to make the search case insensitive.

#1
"parameters": {
"where": '{"username": "' + encodeURIComponent(username) + '"}'
}

#2
{"username": {"$regex": "username$", "$options":"i"}}';

I've tried this but I'm sure the syntax is NOT correct:

"parameters": {
"where": '{"username": {"$regex": "username$", "$options":"i"},
"username": "' + encodeURIComponent(username) + '"}'
}

Thanks!


Query a an exact string while ignoring case

Posted: Thu Aug 07, 2014 1:49 am
by Yurii Orishchuk

Hi R2R,

In this case use this code:

pre

{
where : '{"username": {"$regex": "^' + encodeURIComponent(username) + '$", "$options":"i"}}'
}

/pre

Regards.


Query a an exact string while ignoring case

Posted: Mon Aug 11, 2014 1:29 pm
by R2R

Yurri, this code also worked perfectly for me. Thank you!!