问题描述
例如,如果我的数据库是:
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?
推荐答案
包含(key, substring)
方法是您想要的方法,但请注意,这只会匹配第二个和第四个文档.
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 中查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!