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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With