2013-10-08 39 views
6

查詢例如,如果我的數據庫是:如何做一個「喜歡」在Parse.com

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

我想的東西的效果:query.like("b")並使其返回第一,第二和第四docs

在Javascript API中有這樣的事嗎?

+0

相關:https://www.parse.com/questions/are-like-or-regex-queries-possible-via-the-rest-api –

+0

所以Javascript query.startsWith()函數只會得到「比利」和「鮑勃」。不是「流浪漢」。所以它缺乏這方面的內容。 – bzupnick

回答

8

contains(key, substring)方法是您想要的方法,但請注意,這隻會匹配第二個和第四個文檔。

原因是搜索區分大小寫。常用的戰術是添加雲代碼處理程序搜索內容寫入到另一個領域before save,例如:

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

現在你可以使用query.contains("search_name", searchString.toLowerCase())找到他們。

如果您希望只搜索名字而不是姓氏,可以添加另一個字段(「search_first」),或者根據您的需要修改上述內容。

+0

那麼,在文檔中,我不會丟失案件嗎? – bzupnick

+1

您保留原始的「第一個」和「最後一個」字段,並添加一個附加的「search_name」字段,並將這些值的副本轉換爲小寫字母。 –