Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse.com search with wild character using javascript

I'm trying to create a search function in javascript using parse.com where a field contains a string. I've already search through the documents of parse.com but I can't find something similar to what I want. I want to query something like this:

query.like("contents", "%string%"); // example only this code does not exist.

Can any one know what method in parse I need?

In iOS I find this method whereKey:containsString: but I think containsString is not available in javascript.

like image 434
khatzie Avatar asked Jan 21 '14 08:01

khatzie


1 Answers

There is a way to do this, but the documentation warns that it may timeout on larger data sets. The solution is to add a constraint the query using a regular expression. Use the query constraint matches(key, regex).

Assuming there is a class named "Post" and it has a string attribute named "contents", then:

var query = new Parse.Query(Post);
query.matches("contents", ".*string.*");
query.find()

To AND multiple conditions, add more query matches constraints:

var query = new Parse.Query(Post);
query.matches("contents", ".*string.*");
query.matches("otherAttribute", ".*otherString.*");
query.find()

Using the Parse.com JavaScript SDK, it is possible to use a logical OR:

var redQuery = new Parse.Query("Post");
redQuery.equalTo("color", "red");

var blueQuery= new Parse.Query("Post");
blueQuery.equalTo("color", "blue");

var mainQuery = Parse.Query.or(redQuery, blueQuery);
like image 200
ahoffer Avatar answered Nov 03 '22 02:11

ahoffer