问题描述
例如,如果我的数据库是:
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"}]
}
我想要一些效果: query.like(b)
并让它返回第一个,第二个和第四个文档
I would like something to the effect of: query.like("b")
and have it return the first, second and fourth docs
Javascript API中是否有这样的内容?
Is there anything like this in the Javascript API?
推荐答案
方法是您想要的方法,但请注意,这只会匹配第二个和第四个文档。
The contains(key, substring)
method is the one you want, but note that this will only match on the second and fourth docs.
原因是搜索区分大小写。一种常见的策略是添加一个Cloud Code处理程序,将可搜索的内容写入另一个字段,例如:
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();
});
现在你可以使用 query.contains(search_name,searchString.toLowerCase ())
找到它们。
Now you can use query.contains("search_name", searchString.toLowerCase())
to find them all.
如果你想只搜索名字而不是姓氏,或者添加另一个字段(search_first),或者根据需要修改上面的字段。
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.
这篇关于如何制作一个“喜欢”的在Parse.com中查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!