5

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.

2
  • Do you mean something like indexOf, but with wildcard? Commented Jan 21, 2014 at 8:53
  • no I'm referring to parse query. Commented Jan 21, 2014 at 8:54

1 Answer 1

6

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);
Sign up to request clarification or add additional context in comments.

2 Comments

what if i want to find matches in 2 column? creating 2 query.matches will return to AND i want to find matches either of the 2 fields. how can i do that?
It's weird matches doesn't appear in their doc's Javascript session, where did you find that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.