Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

javascript - How to make a "like" query in Parse.com

For example, If my database is:

{
    people: name: [{"first":"Billy", "last":"smith"}]
},
{
    people: name: [{"first":"bob", "last":"smith"}]
},
{
    people: name: [{"first":"thor", "last":"smith"}]
},
{
    people: name: [{"first":"hobo", "last":"smith"}]
}

I would like something to the effect of: query.like("b") and have it return the first, second and fourth docs

Is there anything like this in the Javascript API?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The contains(key, substring) method is the one you want, but note that this will only match on the second and fourth docs.

The reason is that searches are case-sensitive. A common tactic is to add a Cloud Code handler to write searchable content to another field before save, e.g.:

Parse.Cloud.beforeSave("people", function(request, response) {
    request.object.set(
        "search_name", 
        request.object.get("first").toLowerCase() 
            + " " 
            + request.object.get("last").toLowerCase()
    );
    response.success();
});

Now you can use query.contains("search_name", searchString.toLowerCase()) to find them all.

If you want to be able to search on just the first-name and not the last-name, either add another field ("search_first"), or just modify the above, depending on your needs.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...