How to make a “like” query in Parse.com

后端 未结 1 424
花落未央
花落未央 2020-12-20 18:09

For example, If my database is:

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


        
相关标签:
1条回答
  • 2020-12-20 18:40

    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.

    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题